Android uses global variables to pass data. android global variables
In android, the Application is used to save global variables. It exists when the package is created and is released only after all the activities are destroy. So when we need global variables, we only need to implement them in the application, and obtain an Application object by calling the getApplicationContext of Context or the getApplication method of Activity, you can set or read the value of the global variable.
When the Application is started, the system creates a PID, that is, the process ID, and all the activities will run on this process. Then we initialize the global variables when the Application is created. All the activities of the same Application can obtain the values of these global variables. In other words, if we change the values of these global variables in an Activity, the values of other activities in the same application will change.
Instance test: If you set the content in the input to a global variable, there are two scenarios.
The specific implementation method is as follows:
1. Create a shared global variable
Create a WirelessApp class with shared variables and inherit the Application
Java code
- Package com. wirelessqa. testintent;
- Import android. app. Application;
- /**
-
- * Inherit application and set global variables
-
- * @ Author bixiaopeng 2013-2-18 11:32:19 AM
-
- */
- Public class WirelessApp extends Application {
- Private String username;
- Public String getUsername (){
- Returnusername;
- }
- Public void setUsername (String username ){
- This. username = username;
- }
- }
2. Configure AndroidMainifest. xml
Declare the global variable class in AndroidMainifest. xml, then Android will create a global available instance
Set android: name = ". WirelessApp" in the Application attribute"
3. Call global variables
You can use Content. getApplicationConext () anywhere else to obtain this instance and then obtain global variables.
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 ();
- // Obtain the application instance
- WirelessApp app = (WirelessApp) getApplicationContext ();
- // Assign a value to the global variable
- App. setUsername (result );
- // Start another activity
- Intent intent = new Intent (MainActivity. this, ResultActivity. class );
- StartActivity (intent );
- }
- });
- }
- }
4. Call the global variable value
Java code
- Package com. wirelessqa. testintent;
- Import android. app. Activity;
- Import android. OS. Bundle;
- Import android. widget. TextView;
- /**
- * Activity that displays the result
- *
- * @ Author bixiaopeng 2013-2-18 11:29:50 AM
- */
- Public class ResultActivity extends Activity {
- Private TextView text = null;
- @ Override
- Protected void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. activity_result );
- // Obtain the application instance
- WirelessApp app = (WirelessApp) getApplicationContext ();
- String result = app. getUsername (); // Value
- Text = (TextView) findViewById(R.id.txt _ result );
- Text. setText (result );
- }
- }
In android, how does one use intent to transmit data by passing variables?
Private static final String DATABASE_NAME = "test. db ";
Private static final int DATABASE_VERSION = 1;
Public class DatabaseHelper extends SQLiteOpenHelper {
Public DatabaseHelper (Context context ){
Super (context, DATABASE_NAME, null, DATABASE_VERSION );
}
In this way, you can define the parameters first. You can try again. I am also engaged in android development. If you have any questions, please contact me. My QQ account is 379371398 and I hope to adopt it.
How to use global variables in Android
Check whether global variables can be used in android. The Java developer must have used global variables. The usage is nothing more than defining a static variable and the public type, so that you can directly call it in other classes.