How to use the "Android data transfer" global variable Indian old Jock 2013-02-18 21:36:14Application 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 a global variable, 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
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
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
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 an Application instance
Wirelessapp app = (Wirelessapp) getapplicationcontext ();
String result = App.getusername ();//value
Text = (TextView) Findviewbyid (R.id.txt_result);
Text.settext (result);
}
}
SOURCE Download: http://pan.baidu.com/share/link?shareid=266061&uk=436271564
Original link: http://www.wirelessqa.com/?p=192
Null
How to use the "Android data transfer" global variable