為了保存軟件的參數(shù),或者是某些比較小型的數(shù)據(jù),Android中我們可以使用Android為我么提供的SharedPreference類他是一個輕量級的儲存類,特別適合用于保存軟件參數(shù)使用SharedPreference保存數(shù)據(jù),其背后是使用xml文件存放數(shù)據(jù)
文件存放在/data/data/<-package name->/shared_prefs目錄下
實例
<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="寫入數(shù)據(jù)" android:layout_alignParentTop="true" 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="讀取數(shù)據(jù)" 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 { //根據(jù)key讀取數(shù)據(jù) private SharedPreferences preferences; //寫入數(shù)據(jù) private SharedPreferences.Editor editor; private Button write,read; private EditText editText; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { 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 onClick(View v) { editor.putString("name",editText.getText().toString()); editor.commit(); editText.setText(""); } }); read.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name=preferences.getString("name",null); textView.setText(name); } }); } @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); } }
在其他的應用程序中訪問
public class ReadOtherPreferences extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Context context =null; try{ //獲取其他應用程序對應的Context context=createPackageContext("xiaocool.net.sharedpreferencestest",Context.CONTEXT_IGNORE_SECURITY); }catch (PackageManager.NameNotFoundException e){ e.printStackTrace(); } //使用其他程序的Context 獲取對應的.sharedpreferences SharedPreferences sharedPreferences=context.getSharedPreferences("YU",Context.MODE_WORLD_READABLE); //讀取數(shù)據(jù) String name=sharedPreferences.getString("name",null); Toast.makeText(ReadOtherPreferences.this,name,1).show(); } }
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網(wǎng)頁題目:Android數(shù)據(jù)存儲與IO(一)-創(chuàng)新互聯(lián)
網(wǎng)站鏈接:http://bm7419.com/article6/dihgog.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供域名注冊、標簽優(yōu)化、微信公眾號、網(wǎng)頁設計公司、定制網(wǎng)站、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容