Save to Sharepreference
1. Get Sharepreference
Getsharepreferences (); The name parameter is specified by the first parameter and can be executed in any context in the app
Getpreferences (); no need to provide file name
For example, an example executed in a fragment:
Context context == context.getsharedpreferences ( getString (R.string.preference_file_key), Context.mode_private);
You can use the Getpreferences () method when the activity requires only a shared preferences file
Sharedpreferences sharedpref = getactivity (). Getpreferences (Context.mode_private);
Note: Private mode can only be accessed by our app
If you create a shared preference file with a mode_world_readable or mode_world_writeable mode,
Any other app can access the file by its file name.
2. Create a sharedpreferences.editor by executing edit ()
Sharedpreferences.editor Editor = Sharedpref.edit ();
3. Transfer keys and values by means similar to Putint () and putstring ()
Editor.putint (GetString (R.string.saved_high_score), newhighscore);
4. Finally commit changes via commit ()
Editor.commit ();
Update manifest file
<activity android:name= ". UI. MyActivity "> <intent-filter> <action android:name=" Android.intent.action.SEND "/> <category Andro Id:name= "Android.intent.category.DEFAULT"/> <data android:mimetype= "image/*"/> </intent-filter> < ;intent-filter> <action android:name= "Android.intent.action.SEND"/> <category android:name= "android.in Tent.category.DEFAULT "/> <data android:mimetype=" Text/plain "/> </intent-filter> <INTENT-FILTER&G T <action android:name= "Android.intent.action.SEND_MULTIPLE"/> <category android:name= " Android.intent.category.DEFAULT "/> <data android:mimetype=" image/* "/> </intent-filter></ Activity>
Processing the received data
voidonCreate (Bundle savedinstancestate) {...//Get Intent, action and MIME typeIntent Intent =getintent (); String Action=intent.getaction (); String type=Intent.gettype (); if(Intent.ACTION_SEND.equals (ACTION) && type! =NULL) { if("Text/plain". Equals (Type) {Handlesendtext (intent); //Handle text being sent}Else if(Type.startswith ("image/") {handlesendimage (intent); //Handle single image being sent } } Else if(Intent.ACTION_SEND_MULTIPLE.equals (ACTION) && type! =NULL) { if(Type.startswith ("image/") {handlesendmultipleimages (intent);//Handle multiple images being sent } } Else { //Handle Other intents, such as being started from the home screen } ... } voidHandlesendtext (Intent Intent) {String Sharedtext=Intent.getstringextra (Intent.extra_text); if(Sharedtext! =NULL) { //Update UI to reflect text being shared } } voidhandlesendimage (Intent Intent) {Uri Imageuri=(Uri) Intent.getparcelableextra (Intent.extra_stream); if(Imageuri! =NULL) { //Update UI to reflect image being shared } } voidhandlesendmultipleimages (Intent Intent) {ArrayList<Uri> Imageuris =Intent.getparcelablearraylistextra (Intent.extra_stream); if(Imageuris! =NULL) { //Update UI to reflect multiple images being shared } }
Data storage for Android development (i)