Data needs to be shared among multiple activities or services
1. Custom Application
JavaCode
Public class demoapplication extends application {private hashmap <string, Object> map = new hashmap <string, Object> (); Public void put (string key, object) {map. put (Key, object);} public object get (string key) {return map. get (key );}}
2. Modify the androidmanifest. xml file:
XML Code
<ApplicationAndroid: Name = "demoapplication"Android: icon = "@ drawable/APP" Android: Label = "@ string/app_name">
Pay attention to the content marked in bold and black, specifyProgramUse our own defined application.
3. Usage:
In one activity, save the data to the map defined in the application.
Java code
Demoapplication APP = (demoapplication) getapplication (); app. Put ("key", "this is a test! ");
Then retrieve data in another activity:
Java code
Demoapplication APP = (demoapplication) getapplication (); string value = (string) app. get ("key"); toast. maketext (this, value, toast. length_long ). show ();
Note: concurrency is not considered here. If necessary, you can add the relevant synchronization code in the Custom Application for implementation.