Usage scenarios:
Need to share data between multiple activity or service
1. Custom Application
Public class extends Application { privatenew hashmap<string, object>(); Public void put (String Key,object object) { map.put (key, object); } Public Object get (String key) { return map.get (key); }}
2. Modify the Androidmanifest.xml file:
<application <strong><em>android:name= "DemoApplication" </em></strong> android:icon= "@ Drawable/app "android:label=" @string/app_name ">
Note the contents of the black bold character, specifying that the program uses our own defined application.
3. Use:
Save data to a map defined in application in one of the activity
DemoApplication app = (demoapplication) getapplication () app.put ("key", "This is a test!");
Then get the data in another activity:
DemoApplication app == (String) app.get ("key"); Toast.maketext (This, value, Toast.length_long). Show ();
Note that concurrency issues are not considered here and, if needed, can be implemented by adding the associated synchronization code to the custom application.
Use of Android global variables