Data storage for Android development (i)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.