Application in Android is used to save global variables, and it exists when the package is created, and is released when all activity is destroy off. So when we need global variables, we just have to implement them in application, You can set or read the value of a global variable by invoking the Getapplicationcontext of the context or the Getapplication method of the activity to obtain a Application object.
When application is started, a PID, the process ID, is created, and all activity is run on this process. Then we initialize the global variables when the application is created, all the activity of the same application can fetch the values of these global variables, in other words, we change the values of these global variables in one activity, Then the value of the other activity in the same application will change.
Example: If the input content is set to global variables, then there are two cases.
The implementation method is as follows:
I. Create a new Shared global variable
Create a new class of shared variables Wirelessapp, you need to inherit application
Java code
- Package com.wirelessqa.testintent;
- Import android.app.Application;
- /**
- * Inherit application, set global variables
- * @author Bixiaopeng 2013-2-18 11:32:19
- */
- Public class Wirelessapp extends application {
- Private String username;
- Public String GetUserName () {
- Returnusername;
- }
- Public void Setusername (String username) {
- This.username = Username;
- }
- }
Two. Configure Androidmainifest.xml
When you declare a class of global variables in Androidmainifest.xml, Android creates a globally available instance
Set Android:name= "In the Application property . Wirelessapp "
Three. Calling Global variables
You can use Content.getapplicationconext () anywhere else to get this instance, and then get the global variable
Java code
- Package com.wirelessqa.testintent;
- 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;
- Public class Mainactivity extends Activity {
- Private EditText edit = null;
- Private button button = null;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- Super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- Edit = (EditText) Findviewbyid (R.id.edit);
- Button = (button) Findviewbyid (R.id.btn_submit);
- Button.setonclicklistener (new Onclicklistener () {
- @Override
- Public void OnClick (View v) {
- String result = Edit.gettext (). toString ();
- Get an Application instance
- Wirelessapp app = (Wirelessapp) getapplicationcontext ();
- Assigning a value to a global variable
- App.setusername (result);
- Start another activity
- Intent Intent = new Intent (mainactivity. This, resultactivity. class);
- StartActivity (Intent);
- }
- });
- }
- }
Four. Calling the value of a global variable
Java code
- Package com.wirelessqa.testintent;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import Android.widget.TextView;
- /**
- * Activity that shows results
- *
- * @author Bixiaopeng 2013-2-18 11:29:50
- */
- Public class Resultactivity extends Activity {
- private TextView text = null;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_result);
- //Get Application instances
- Wirelessapp app = (Wirelessapp) getapplicationcontext ();
- String result = App.getusername (); //value
- Text = (TextView) Findviewbyid (R.id.txt_result);
- Text.settext (result);
- }
- }
Android uses global variables to pass data