Android Data storage------2 shared parameters

Source: Internet
Author: User

To better validate the code, we first change the Androidmanifest.xml, add the unit test

1   < Instrumentation 2         Android:name = "Android.test.InstrumentationTestRunner" 3         android:targetpackage= "com.yxh.androidshareprefences01">4     </instrumentation>

and add <uses-library android:name= "Android.test.runner" in <application>/>

1  <Application2         Android:allowbackup= "true"3 Android:icon= "@drawable/ic_launcher"4 Android:label= "@string/app_name"5 Android:theme= "@style/apptheme" >6         <uses-libraryAndroid:name= "Android.test.runner" />

Then make the formal code:
The first step: Write the MySharedPreferences01 class for saving read files

1  Packagecom.yxh.androidshareprefences01;2 3 4 ImportJava.util.HashMap;5 ImportJava.util.Map;6 7 Importandroid.app.Activity;8 ImportAndroid.content.Context;9 Importandroid.content.SharedPreferences;Ten  One  Public classMyShareprefences01 { A     Privatecontext context; -  -     //Construction Method the      PublicMyShareprefences01 (Context context) { -          This. context=context; -          -     } +     //create a method for storing information -      Public Booleansaveshareprefences (String name,string age) { +         Booleanflag=false; A         //Instantiate Sharedpreferences atSharedpreferences share=context.getsharedpreferences ("info", activity.mode_private); -         //call the interface under Sharedpreferences to write -Sharedpreferences.editor editor=Share.edit (); -         //Write Information -Editor.putstring ("name", name); -Editor.putstring ("Age", age); in         //submit information to persist data - editor.commit (); toflag=true; +         returnFlag; -     } the      PublicMap<string, String >readshareprefences () { *         //instantiate a map for receiving information $Map<string, String > map=NewHashmap<string, string>();Panax Notoginseng         //Instantiate Sharedpreferences -Sharedpreferences share=context.getsharedpreferences ("info", activity.mode_private); the         //get information based on key value +String name=share.getstring ("name", "No"); AString age=share.getstring ("Age", "0"); the         //to map the information obtained +Map.put ("name", name); -Map.put ("Age", age); $      $         returnmap; -     } -      the  -}

The second step is to test the code

1  Packagecom.yxh.androidshareprefences01;2 3 ImportJava.util.Map;4 5 Importandroid.app.Activity;6 ImportAndroid.content.Context;7 Importandroid.content.SharedPreferences;8 Importandroid.test.AndroidTestCase;9 ImportAndroid.util.Log;Ten  One  Public classMyTestextendsAndroidtestcase { A     Private StaticString tag= "MyTest"; -  -      PublicMyTest () { the         //TODO auto-generated Constructor stub -     } -     //test data to save data -      Public voidsaveshareprefences () { +Context context=GetContext (); -MyShareprefences01 share=NewMyShareprefences01 (context); +        BooleanFlag=share.saveshareprefences ("Neusoft", "100"); ALOG.I (TAG, "---->>" +flag); at     } -     //test for read Data -      Public voidreadshareprefences () { -Context context=GetContext (); -MyShareprefences01 share=NewMyShareprefences01 (context); -Map<string,string> map=share.readshareprefences (); inLOG.I (TAG, "---->>" +map.tostring ()); -         } to  +}

Test results:

1 10-26 01:55:58.876:i/mytest (3401):---->>true2 10-26 02:02:42.716:i/mytest (5120) :---->>{age=100, Name=neusoft}

Android Data storage------2 shared parameters

Related Article

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.