The interaction between the activity class and the HTML page of the Android Hybrid development (and solve the black screen problem)
At the bottom of the program source code download
The program flow: Program Start-->testactivity--->PHONEGAP2 framework class--->index.html--->testactivity, the main implementation of activity and HTML page mutual jump, and implement the function of passing parameters.
Program Structure diagram:
1. Create an Android project, add the PHONEGAP framework to the project (click here for details), we know that we often use activity when defining a main interface, here we define a testactivity, process
The sequence code is as follows:
Package Com.myphonegap;import Android.app.activity;import Android.content.intent;import android.os.Bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.textview;public class Testactivity extends Activity {private EditText Edittext;private button button, @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stubsuper.oncreate (Savedinstancestate); Setcontentview (r.layout.main); edittext = (edittext) Findviewbyid ( R.ID.EDITTEXT1); button = (button) Findviewbyid (r.id.button1);//Receive HTML page parameter string str = Getintent (). Getstringextra (" Name "); String str1 = Getintent (). Getstringextra ("name");//Set the text content of the edit box to receive the value Edittext.settext (STR+STR1);// Set the binding event for the button Button.setonclicklistener (new Onclicklistener () {public void OnClick (View v) {//Set intent jump between intent intent = New Intent (Testactivity.this,phonegap2activity.class);//Start intentstartactivity (Intent);}});}}
2. In Phonegap2activity, this class inherits the Droidgap class, which makes it easy to jump to an HTML page in this activity. Which means the activity jumps to
Inside an HTML page. Then the content of the HTML page after the jump is displayed. I'm thinking about how to get back to testactivity from the post-jump HTML page, where it involves JS calling Java
Code, in fact, the same as Appview.addjavascriptinterface (obj,string str) added a JS operation Java interface can be, the first parameter is an instance of the class, the second parameter is called
The instance of the JS name.
Here is the Phonegap2activity code:
Package Com.myphonegap;import Org.apache.cordova.droidgap;import Android.content.intent;import android.os.Bundle; public class Phonegap2activity extends Droidgap {/** Called when the activity is first created. */string str; @Overridepubl IC void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Super.loadurl ("File:///android_ Asset/www/index.html "); In this method add JS Operation Java interface, this is the current object, JS1 for the operation of Java file JavaScript name appview.addjavascriptinterface (This, "js1");} public void Method (String str,string str1) {Intent Intent = new Intent (); Intent.putextra ("name", str); Intent.putextra (" Pass ", str); Intent.setclass (Phonegap2activity.this, Testactivity.class); startactivity (intent);}}
This time you will encounter a black screen problem: that is, when the activity jumps to the delay between HTML, to solve this problem, you need to add a few lines of code:
Super.init (); This.appView.setBackgroundResource (r.drawable.load);//Set background picture Super.setintegerproperty ("SplashScreen", R.drawable.load); Set the splash screen background image Super.loadurl ("file:///android_asset/www/login.html"); Tested 3000 milliseconds more appropriate
Above solve the black screen key code
The modified code is:
Package Com.myphonegap;import Org.apache.cordova.droidgap;import Android.content.intent;import android.os.Bundle; public class Phonegap2activity extends Droidgap {/** Called when the activity is first created. */string str; @Overridepubl IC void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Super.init (); This.appView.setBackgroundResource (r.drawable.load);//Set background picture Super.setintegerproperty ("SplashScreen", R.drawable.load); Set the splash screen background image Super.loadurl ("file:///android_asset/www/login.html"); Tested 3000 milliseconds more appropriate //Add JS operation Java interface In this method, this is the current object, JS1 is the name of the JavaScript to manipulate Java files Appview.addjavascriptinterface (This, "js1");} public void Method (String str,string str1) {Intent Intent = new Intent (); Intent.putextra ("name", str); Intent.putextra (" Pass ", str); Intent.setclass (Phonegap2activity.this, Testactivity.class); startactivity (intent);}}
3. This HTML page is a jump HTML page, it through the definition of the JS function directly call the Java method, through the JS call phonegap2activity method, so that the HTML page for
Testactivity jump function. ----Note the introduction of the package
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
Note: For the above example, if you debug with a virtual machine, when the virtual machine version is 2.3, there may be no results, the program is not a problem, because this is the version of the virtual machine bug, the version is changed to 2.2 can be solved
This bug. Of course, if you use your phone to debug, then there is no problem.
Run:
The initialization page is as follows:
The initial value is NULL, click the button to enter the HTML page, enter the user name and password, such as:
Click the button to return to Testactivity, as
SOURCE Download: http://pan.baidu.com/share/link?shareid=602760443&uk=2418081758
This article from: http://www.myexception.cn/HTML-CSS/1403290.html