</pre><pre>
Package Com.example.applicationtest;import android.app.application;/** * Created to Chang on 14-10-1. */public class App extends application {public String getName () { return name; } public void SetName (String name) { this.name = name; } public String name; @Override public void OnCreate () { super.oncreate (); SetName ("Zhang San");} }
Package Com.example.applicationtest;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.view.view;import Android.widget.button;public class MyActivity extends Activity { private Button btnlaunch = null; Private App myApp; /** * Called when the activity is first created. * /@Override public void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.main); Btnlaunch = (Button) This.findviewbyid (r.id.btnlaunch); MYAPP = (App) this.getapplication (); Myapp.setname ("Hello"); Btnlaunch.setonclicklistener (New View.onclicklistener () { @Override public void OnClick (view view) { Intent Intent = new Intent (myactivity.this,otheractivity.class); StartActivity (intent);}} );} }
Package Com.example.applicationtest;import Android.app.activity;import Android.os.bundle;import android.widget.textview;/** * Created by Chang on 14-10-1. */public class Otheractivity extends Activity { private App myApp; Private TextView TextView; @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.other); TextView = (TextView) Findviewbyid (r.id.showmsg); MYAPP = (App) getapplication (); Textview.settext (Myapp.getname ());} }
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android " package=" com.example.ApplicationTest " android:versioncode=" 1 " android:versionname=" 1.0 " > <uses-sdk android:minsdkversion= "/> <application android:label=" @string/app_name " android:icon= "@drawable/ic_launcher" android:name= ". App > <activity android:name= "myactivity" 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= ". Otheractivity "> </activity> </application></manifest>
Android passes data via application