There is something similar to the session on Android, called Application.
There is something similar to the session on Android, called Application.
1, you can create a new class, for example: Hellowordapplication.java must extends application (name with you)
In this class, set the data variables you want to be global, for example: private String loginName;
It then generates its get, set method.
2. Configure your application class in the Androidmanifest.xml file as follows:
<application android:icon= "@drawable/icon" android:label= "@string/app_name" Android:name= "hellowordapplication ">
<activity >
Slightly
</activity>
</application>
is to add the attribute configuration of android:name= "Hellowordapplication" to the <application> tag.
3, in the use of the time:
Hellowordapplicationapplication = (hellowordapplication) getapplication ();
Application.setloginname ("Baidu");
Then will "Baidu" saved to the application, other places to use when, application.getloginname ();
Android Getapplication () use
Java code
- Package com.hyzing;
- Import android.app.Application;
- Public class Mysystemappcation extends application{
- private int curindex;
- public int Getcurindex () {
- return curindex;
- }
- public void Setcurindex (int curindex) {
- this.curindex = Curindex;
- }
- @Override
- public void OnCreate () {
- super.oncreate ();
- }
- @Override
- public void Onterminate () {
- super.onterminate ();
- }
- }
Java code
- Package com.hyzing;
- Import android.app.Activity;
- Import android.content.Intent;
- Import Android.os.Bundle;
- Import Android.util.Log;
- Public class Systemtestactivity extends Activity {
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- //setcontentview (R.layout.main);
- Mysystemappcation application = (mysystemappcation) This
- . Getapplication ();
- System.out.println ("++++++++++++++" +application);
- LOG.I ("Data", "===============" + Application.getcurindex ());
- Application.setcurindex (5);
- Intent Intent = new Intent ();
- Bundle bundle = new bundle ();
- Bundle.putstring ("CheckIn", "0");
- Bundle.putboolean ("Manageruser", true);
- Intent.putextras (bundle);
- Intent.setclass (<span style="color: #ff0000;" >systemtestactivity. This</span>, printactivity. class);
- StartActivity (Intent);
- }
- }
Java code
- Package com.hyzing;
- Import android.app.Activity;
- Import android.content.Intent;
- Import Android.os.Bundle;
- Import Android.util.Log;
- Public class Printactivity extends Activity {
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- <span style="color: #ff0000;" >super.oncreate (savedinstancestate);</span>
- Mysystemappcation application = (mysystemappcation) This
- . Getapplication ();
- LOG.I ("Data", " " "+ Application.getcurindex ());
- Application.setcurindex (6);
- Intent Intent = new Intent ();
- Bundle bundle = new bundle ();
- Bundle.putstring ("CheckIn", "0");
- Bundle.putboolean ("Manageruser", true);
- Intent.putextras (bundle);
- Intent.setclass (printactivity. This, printagainactivity. class);
- StartActivity (Intent);
- }
- }
Java code
- Package com.hyzing;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import Android.util.Log;
- Public class Printagainactivity extends Activity {
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- <span style="color: #ff0000;" >super.oncreate (savedinstancestate);</span>
- Final Mysystemappcation application = (mysystemappcation) This
- . Getapplication ();
- LOG.I ("Data", " " "+ Application.getcurindex ());
- }
- }
Java code
- <?xml version="1.0" encoding="Utf-8"?>
- <manifest xmlns:android="Http://schemas.android.com/apk/res/android"
- package="com.hyzing"
- android:versioncode="1"
- Android:versionname="1.0" >
- <uses-sdk android:minsdkversion="/> "
- <application android:name=". Mysystemappcation "
- android:icon="@drawable/ic_launcher"
- Android:label="@string/app_name" >
- <activity
- Android:name=". Systemtestactivity "
- Android:label="@string/app_name" >
- <intent-filter>
- <action android:name="Android.intent.action.MAIN"/>
- <category android:name="Android.intent.category.LAUNCHER"/>
- </intent-filter>
- </activity>
- <activity
- Android:name=". Printactivity "
- Android:label="@string/app_name" >
- </activity>
- <activity
- Android:name=". Printagainactivity "
- Android:label="@string/app_name" >
- </activity>
- </application>
- </manifest>
There is something similar to the session under Android, called Application, getapplication () usage