Android data storage with IO (i)

Source: Internet
Author: User

1.Sharedpreference Introduction

In order to save the parameters of the software, or some smaller data, we can use the Sharedpreference class that Android provides for me. He is a lightweight storage class, Ideal for saving software parameters using Sharedpreference to save data, behind which data is stored using an XML file
Files are stored in the/data/data/<-package name->/shared_prefs directory

Instance

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"      xmlns:tools= "Http://schemas.android.com/tools"  android:layout_width= "Match_parent"      android:layout_height= "Match_parent"  android:paddingleft= "@dimen/activity_horizontal_margin"      android:paddingright= "@dimen/activity_horizontal_margin"     android: paddingtop= "@dimen/activity_vertical_margin"     android:paddingbottom= "@dimen/activity_ Vertical_margin " tools:context=". Mainactivity ">   <button       android:id=" @+id/ Buttonwrite "       android:layout_width=" Wrap_content "        android:layout_height= "Wrap_content"        android: text= "Write Data"        android:layout_alignparenttop= "true"    &Nbsp;   android:layout_centerhorizontal= "true"  />    <edittext         android:id= "@+id/edit"          android:layout_width= "Fill_parent"         android:layout_ height= "Wrap_content"         android:layout_below= "@+id/buttonWrite"         android:layout_alignparentleft= "true"          android:layout_alignparentstart= "true"  />    <button         android:id= "@+id/buttonreade"          android:layout_width= "Wrap_content"         android: layout_height= "Wrap_content"         android:text= "read Data"          android:layout_below= "@+id/edit"         android:layout_alignleft = "@+id/buttonwrite"         android:layout_alignstart= "@+id/buttonWrite "        android:layout_margintop=" 73DP " />     <textview        android:id= "@+id/textview"          android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"         android:text= "Yu"         android:textsize= "20SP"          android:layout_alignparentbottom= "true"         android: Layout_centerhorizontal= "true"         android:layout_marginbottom= " 167DP " /></relaTivelayout> 
package xiaocool.net.sharedpreferencestest;import android.content.sharedpreferences;import  Android.support.v7.app.actionbaractivity;import android.os.bundle;import android.view.menu;import  android.view.MenuItem;import android.view.View;import android.widget.Button;import  Android.widget.edittext;import android.widget.textview;public class mainactivity extends  actionbaractivity {    //reading data according to key     private  sharedpreferences preferences;    //Writing Data     private  sharedpreferences.editor editor;    private button write,read;     private EditText editText;    private TextView textView;      @Override     protected void oncreate (bundle  Savedinstancestate)  {    &Nbsp;   super.oncreate (savedinstancestate);         Setcontentview (R.layout.activity_main);         write= (Button) This.findviewbyid (R.id.buttonwrite);         read= (Button) This.findviewbyid (R.id.buttonreade);         edittext= (EditText) This.findviewbyid (R.id.edit);         textview= (TextView) This.findviewbyid (R.id.textview);         preferences= Getsharedpreferences ("Xiaocool", mode_private);         editor= Preferences.edit ();         write.setonclicklistener (new  View.onclicklistener ()  {             @Override             public void&nbsP;onclick (VIEW&NBSP;V)  {                editor.putstring ("Name", Edittext.gettext (). toString ());                 editor.commit ();                 edittext.settext ("");             }        });       read.setonclicklistener (New view.onclicklistener ()  {            @Override            public void onclick (VIEW&NBSP;V)  {               string name=preferences.getstring ("name", null);           &Nbsp;   textview.settext (name);           } &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP);    }     @Override      public boolean oncreateoptionsmenu (Menu menu)  {         // Inflate the menu; this adds items to the  action bar if it is present.         Getmenuinflater (). Inflate (R.menu.menu_main, menu);         return  true;    }     @Override     public  Boolean onoptionsitemselected (Menuitem item)  {        / / handle action bar item clicks here. the action bar will         // automatically handle clicks on the home/up button, so  long        // as you specify a parent  activity in androidmanifest.xml.        int id  = item.getitemid ();        //noinspection  simplifiableifstatement        if  (Id == R.id.action_ Settings)  {            return true;         }        return  super.onoptionsitemselected (item);     }}

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/5B/96/wKiom1UM1rjSBxeWAAEbo_7sXBM143.jpg "title=" Qq20150321102409.png "alt=" Wkiom1um1rjsbxewaaebo_7sxbm143.jpg "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/5B/91/wKioL1UM1_nihuMHAAcGSxFq3-w259.jpg "title=" Qq20150321100908.png "alt=" Wkiol1um1_nihumhaacgsxfq3-w259.jpg "/>



accessing in other applications

public class readotherpreferences extends actionbaractivity {     @Override     protected void oncreate (bundle savedinstancestate)  {         super.oncreate (savedinstancestate);         setcontentview (R.layout.activity_main);         Context context =null;        try{             //get context       for other applications       context=createpackagecontext ("Xiaocool.net.sharedpreferencestest", context.context_ignore_security);        }catch  ( Packagemanager.namenotfoundexception e) {             e.printstacktrace ();                     }         //Use the context  of other programs to get the corresponding .sharedpreferences         sharedpreferences sharedpreferences=context.getsharedpreferences ("YU", context.mode_world_readable)         //Read Data          string name=sharedpreferences.getstring ("name", null);         toast.maketext (readotherpreferences.this,name,1). Show ();     }}


Android data storage with IO (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.