Dynamic files
String file_name = "Tempfile.tmp"; Try { //ReadFileInputStream FIS =Openfileinput (file_name); InputStreamReader ISR=NewInputStreamReader (FIS, "UTF-8"); BufferedReader BR=NewBufferedReader (ISR); StringBuffer SB=NewStringBuffer (); while(Br.readline ()! =NULL) {sb.append (Br.readline ()+ "\ n"); } System.out.println (Sb.tostring ()); //WriteFileOutputStream fos = openfileoutput (file_name, context.mode_private);//For Other application components to be accessible, use the mode_world_writeableFile File =NewFile (file_name); if(!file.exists ()) { byte[] os = "Write file". GetBytes ("UTF-8");//"This example tests the file write". GetBytes ("GB2312");fos.write (OS); } } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(unsupportedencodingexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }
Static file Resources
// The ID is generated in Gen's R.java. The project hierarchy is Res/raw inputstream is = getresources (). Openrawresource (R.raw.info ); InputStreamReader ISR; New InputStreamReader (IS, "UTF-8"); New BufferedReader (ISR); = ""; while NULL { log.i (TAG, instring); }
Preference Items
intMinimummagnitude = 0;BooleanAutoUpdate =false;intUpdatefreq = 0;//ReadContext context =Getapplicationcontext (); Sharedpreferences prefs=preferencemanager.getdefaultsharedpreferences (context); Minimummagnitude= Integer.parseint (prefs.getstring (Preferences.pref_min_mag, "0")); Updatefreq= Integer.parseint (prefs.getstring (Preferences.pref_update_freq, "0")); AutoUpdate= Prefs.getboolean (Preferences.pref_auto_update,false);//WriteContext context =Getapplicationcontext (); Prefs=preferencemanager.getdefaultsharedpreferences (context); Editor Editor=Prefs.edit (); Editor.putboolean (Pref_auto_update, autoupdatechecked); Editor.putint (Pref_update_freq, Updateindex); Editor.putint (Pref_min_mag, Minmagindex); Editor.commit ();
Preferences interface is inherited preferencesactivity, you only need:
@res/xml/Userpreferences.xml<?xml version= "1.0" encoding= "Utf-8"?><Preferencescreen xmlns:android= "Http://schemas.android.com/apk/res/android" > <checkboxpreference Android:key= "Pref_auto_update"Android:title= "Auto Refresh"android:summary= "Select to turn on automatic updating"Android:defaultvalue= "true"/> <listpreference Android:key= "Pref_update_freq"Android:title= "Refresh Frequency"android:summary= "Frequency at which to refresh earthquake list"android:entries= "@array/update_freq_options"android:entryvalues= "@array/update_freq_values"Android:dialogtitle= "Refresh Frequency"Android:defaultvalue= "/>" <listpreference Android:key= "Pref_min_mag"Android:title= "Minimum magnitude"android:summary= "Select the minimum magnitude earthquake to report"android:entries= "@array/magnitude_options"android:entryvalues= "@array/magnitude"Android:dialogtitle= "Magnitude"Android:defaultvalue= "3"/></preferencescreen>@PreferencesActivity Public voidonCreate (Bundle icicle) {Super. OnCreate (Icicle); Addpreferencesfromresource (r.xml.userpreferences); }
Otherwise, you need to manually set the control's state value in preferences, including the action callbacks that are performed by the OK and cancel keys.
Monitor the change of preference
@Activity Public void onCreate (Bundle icicle) { = getapplicationcontext (); = preferencemanager.getdefaultsharedpreferences (context); Prefs.registeronsharedpreferencechangelistener (this); Public void onsharedpreferencechanged (sharedpreferences prefs, String key) {}
Read and write from the preference of activity
PrivateListView Mylistview;PrivateEditText Myedittext;Private Static FinalString Text_entry_key = "Text_entry_key";Private Static FinalString Adding_item_key = "Adding_item_key"; Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //Inflate your viewSetcontentview (R.layout.main); //Get references to UI widgetsMylistview =(ListView) Findviewbyid (R.id.mylistview); Myedittext=(EditText) Findviewbyid (R.id.myedittext); Restoreuistate (); }Private voidrestoreuistate () {//Get the Activity preferences object.Sharedpreferences settings = getpreferences (0); //Read The UI state values, specifying default values.String Text = settings.getstring (Text_entry_key, ""); Boolean adding= Settings.getboolean (Adding_item_key,false); //Restore the UI to the previous state. if(Adding) {AddNewItem (); Myedittext.settext (text); }} @Overrideprotected voidOnPause () {Super. OnPause (); //Get the Activity preferences object.Sharedpreferences uistate = getpreferences (0); //Get the Preferences editor.Sharedpreferences.editor Editor =Uistate.edit (); //ADD The UI state preference values.editor.putstring (Text_entry_key, Myedittext.gettext (). toString ()); Editor.putboolean (Adding_item_key, AddingNew); //Commit the preferences.Editor.commit (); }
State
Private Static FinalString Selected_index_key = "Selected_index_key"; @Override Public voidonsaveinstancestate (Bundle outstate) {outstate.putint (Selected_index_key, Mylistview.getselecteditemposition ( )); Super. Onsaveinstancestate (outstate); } @Override Public voidonrestoreinstancestate (Bundle savedinstancestate) {intpos =-1; if(Savedinstancestate! =NULL) if(Savedinstancestate.containskey (selected_index_key)) Pos= Savedinstancestate.getint (Selected_index_key,-1); Mylistview.setselection (POS); }
android-Data Persistence