This article illustrates the application setting of global variables and the use of values for Android programming. Share to everyone for your reference, specific as follows:
/**
* Rewrite application, mainly rewrite the OnCreate method inside, that is, when created,
* We let it initialize some of the values that we saw in Javaeye some time ago, similar to this,
* I made some improvements. It is said that foreign developers are used to initializing some global variables, as if the
global variables are initialized in some classes of activity *, and some exceptions to null pointers will be encountered, of course, I have not encountered.
* If initialized with this method, you can avoid any errors that may occur.
*
* Start Application, he will create a PID, is the process ID, all activity will run on this process.
* So we initialize the global variable when application is created, so all the activity can get these
* global variables, and further, we change the values of these global variables in one activity, So in other activity * is not the value of the
change, this calculation does not calculate the transfer value?
* OK, then the following example we test ...
* @author Yong.wang
* * */public
class MyApplication extends application {
private String name ;
@Override public
void OnCreate () {
super.oncreate ();
SetName (NAME); Initialize global variable
} public
String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
private static final String NAME = "MyApplication";
}
OK, the application is created, but we should add the application MyApplication that will run in the configuration file Applicationmanifest.xml and modify it:
<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android=
"http://schemas.android.com/apk/res/" Android "
package=" Com.hisoft.app "
android:versioncode=" 1 "
android:versionname=" 1.0 ">
< Application android:icon= "@drawable/icon" android:label= "@string/app_name" Android:name= "
. MyApplication "> is here, the default application we used to set to him as our own myapplication
<activity android:name=". Myfirstactivity "
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= ". Mysecondactivity "></activity>
</application>
<uses-sdk android:minsdkversion=" 8 "/ >
</manifest>
When the XML configuration file runs out of Android:name=. MyApplication "", then the process ID is allocated, and below, we're going to run our activity.
public class Myfirstactivity extends activity {
private MyApplication app;
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
App = (myapplication) getapplication (); Get our application MyApplication
log.e ("Myfirstactivityoriginal", App.getname ())//Take the global variable we put into the process and see if it's the value we've set
app.setname ("are cool");//ok, now we're starting to modify the
LOG.E ("Myfirstactivitychanged", App.getname ()); Again, this value changes without
Intent Intent = new Intent ();//More importantly, we can see if the value is initialized in the other activity or the modified
Intent.setclass (this , mysecondactivity.class);
StartActivity (intent);
}
When it's done, we're going to jump to this activity.
public class Mysecondactivity extends activity {
private MyApplication app;
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
App = (myapplication) getapplication (); Get Application
log.e ("Mysecondactivity", App.getname ());//Get Global Value
}
}
OK, look at the value: Of course I've already run it.
Myfirstactivityoriginal MyApplication
Myfirstactivitychanged is cool
Mysecondactivity is cool
Look, is not particularly exciting, of course, you can quit after the run, the 2nd, 3 ... Times, it is possible that 3 outputs are all cool, which is the problem that you did not kill the process.
I hope this article will help you with the Android program.