最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android使用SharedPreferences進行數(shù)據(jù)存儲

 更新時間:2017年02月22日 14:13:54   作者:Ruthless  
Android平臺給我們提供了一個SharedPreferences類,它是一個輕量級的存儲類,特別適合用于保存軟件配置參數(shù)。有興趣的可以了解一下。

很多時候我們開發(fā)的軟件需要向用戶提供軟件參數(shù)設置功能,例如我們常用的QQ,用戶可以設置是否允許陌生人添加自己為好友。對于軟件配置參數(shù)的保存,如果是window軟件通常我們會采用ini文件進行保存,如果是j2se應用,我們會采用properties屬性文件或者xml進行保存。如果是Android應用,我們最適合采用什么方式保存軟件配置參數(shù)呢?Android平臺給我們提供了一個SharedPreferences類,它是一個輕量級的存儲類,特別適合用于保存軟件配置參數(shù)。使用SharedPreferences保存數(shù)據(jù),其背后是用xml文件存放數(shù)據(jù),文件存放在/data/data/<package name>/shared_prefs目錄下:

SharedPreferences sharedPreferences = getSharedPreferences("ljq", Context.MODE_PRIVATE);

Editor editor = sharedPreferences.edit();//獲取編輯器

editor.putString("name", "林計欽");

editor.putInt("age", 24);

editor.commit();//提交修改 

生成的ljq.xml文件內(nèi)容如下:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>

<map>

 <string name="name">林計欽</string>

 <int name="age" value="24" />

</map>

因為SharedPreferences背后是使用xml文件保存數(shù)據(jù),getSharedPreferences(name,mode)方法的第一個參數(shù)用于指定該文件的名稱,名稱不用帶后綴,后綴會由Android自動加上。方法的第二個參數(shù)指定文件的操作模式,共有四種操作模式,這四種模式前面介紹使用文件方式保存數(shù)據(jù)時已經(jīng)講解過。如果希望SharedPreferences背后使用的xml文件能被其他應用讀和寫,可以指定Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE權限。

另外Activity還提供了另一個getPreferences(mode)方法操作SharedPreferences,這個方法默認使用當前類不帶包名的類名作為文件的名稱。

訪問SharedPreferences中的數(shù)據(jù)

訪問SharedPreferences中的數(shù)據(jù)代碼如下:

SharedPreferences sharedPreferences = getSharedPreferences("ljq", Context.MODE_PRIVATE);

//getString()第二個參數(shù)為缺省值,如果preference中不存在該key,將返回缺省值

String name = sharedPreferences.getString("name", "");

int age = sharedPreferences.getInt("age", 1);

如果訪問其他應用中的Preference,前提條件是:該preference創(chuàng)建時指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE權限。

如:有個<package name>為com.ljq.action的應用使用下面語句創(chuàng)建了preference。

getSharedPreferences("ljq", Context.MODE_WORLD_READABLE);

其他應用要訪問上面應用的preference,首先需要創(chuàng)建上面應用的Context,然后通過Context 訪問preference ,訪問preference時會在應用所在包下的shared_prefs目錄找到preference :

Context otherAppsContext = createPackageContext("com.ljq.action", Context.CONTEXT_IGNORE_SECURITY);

SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("ljq", Context.MODE_WORLD_READABLE);

String name = sharedPreferences.getString("name", "");

int age = sharedPreferences.getInt("age", 0);

如果不通過創(chuàng)建Context訪問其他應用的preference,也可以以讀取xml文件方式直接訪問其他應用preference對應的xml文件,如:

復制代碼 代碼如下:

File xmlFile = new File("/data/data/<package name>/shared_prefs/itcast.xml");//<package name>應替換成應用的包名

案例:

string.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string name="hello">Hello World, SpActivity!</string>
 <string name="app_name">軟件配置參數(shù)</string>
 <string name="name">姓名</string>
 <string name="age">年齡</string>
 <string name="button">保存設置</string>
 <string name="showButton">顯示</string>
</resources>

main.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" 
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <TextView android:layout_width="wrap_content"
   android:layout_height="wrap_content" 
   android:text="@string/name"
   android:textSize="20px"
   android:id="@+id/nameLable" />
  <EditText android:layout_width="80px"
   android:layout_height="wrap_content" 
   android:layout_toRightOf="@id/nameLable"
   android:layout_alignTop="@id/nameLable"
   android:layout_marginLeft="10px"
   android:id="@+id/name" />
 </RelativeLayout>
 <RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <TextView android:layout_width="wrap_content"
   android:layout_height="wrap_content" 
   android:textSize="20px"
   android:text="@string/age"
   android:id="@+id/ageLable" />
  <EditText android:layout_width="80px"
   android:layout_height="wrap_content" 
   android:layout_toRightOf="@id/ageLable"
   android:layout_alignTop="@id/ageLable"
   android:layout_marginLeft="10px"
   android:id="@+id/age" />
 </RelativeLayout>
 <RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <Button android:layout_width="wrap_content"
   android:layout_height="wrap_content" 
   android:text="@string/button"
   android:id="@+id/button" />
  <Button android:layout_width="wrap_content"
   android:layout_height="wrap_content" 
   android:text="@string/showButton"
   android:layout_toRightOf="@id/button"
   android:layout_alignTop="@id/button"
   android:id="@+id/showButton" />
 </RelativeLayout>
 <TextView android:layout_width="fill_parent"
   android:layout_height="wrap_content" 
   android:textSize="20px"
   android:id="@+id/showText" />
</LinearLayout>

package com.ljq.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SpActivity extends Activity {
 private EditText nameText;
 private EditText ageText;
 private TextView resultText;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  
  nameText = (EditText)this.findViewById(R.id.name);
  ageText = (EditText)this.findViewById(R.id.age);
  resultText = (TextView)this.findViewById(R.id.showText);
  
  Button button = (Button)this.findViewById(R.id.button);
  Button showButton = (Button)this.findViewById(R.id.showButton);
  button.setOnClickListener(listener);
  showButton.setOnClickListener(listener);
  
  // 回顯
  SharedPreferences sharedPreferences=getSharedPreferences("ljq123", 
    Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
  String nameValue = sharedPreferences.getString("name", "");
  int ageValue = sharedPreferences.getInt("age", 1);
  nameText.setText(nameValue);
  ageText.setText(String.valueOf(ageValue));
 }
 
 private View.OnClickListener listener = new View.OnClickListener(){
  public void onClick(View v) {
   Button button = (Button)v;
   //ljq123文件存放在/data/data/<package name>/shared_prefs目錄下
   SharedPreferences sharedPreferences=getSharedPreferences("ljq123", 
     Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
   switch (button.getId()) {
   case R.id.button:
    String name = nameText.getText().toString();
    int age = Integer.parseInt(ageText.getText().toString());
    Editor editor = sharedPreferences.edit(); //獲取編輯器
    editor.putString("name", name);
    editor.putInt("age", age);
    editor.commit();//提交修改
    Toast.makeText(SpActivity.this, "保存成功", Toast.LENGTH_LONG).show();
    break;
   case R.id.showButton:
    String nameValue = sharedPreferences.getString("name", "");
    int ageValue = sharedPreferences.getInt("age", 1);
    resultText.setText("姓名:" + nameValue + ",年齡:" + ageValue);
    break;
   }
  }
 };
}

運行結(jié)果

如何訪問其他應用中的Preference

package com.ljq.sp;

import java.io.File;
import java.io.FileInputStream;

import android.content.Context;
import android.content.SharedPreferences;
import android.test.AndroidTestCase;
import android.util.Log;

public class AccessSharePreferenceTest extends AndroidTestCase{
 private static final String TAG = "AccessSharePreferenceTest";
 
 /**
  * 訪問SharePreference的方式一,注:權限要足夠
  * @throws Exception
  */
 public void testAccessPreference() throws Exception{
  String path = "/data/data/com.ljq.activity/shared_prefs/ljq123.xml";
  File file = new File(path);
  FileInputStream inputStream = new FileInputStream(file);
  //獲取的是一個xml字符串
  String data = new FileService().read(inputStream);
  Log.i(TAG, data);
 }
 
 /**
  * 訪問SharePreference的方式二,注:權限要足夠
  * @throws Exception
  */
 public void testAccessPreference2() throws Exception{
  Context context = this.getContext().createPackageContext("com.ljq.activity", 
    Context.CONTEXT_IGNORE_SECURITY);
  SharedPreferences sharedPreferences = context.getSharedPreferences("ljq123", 
    Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
  String name = sharedPreferences.getString("name", "");
  int age = sharedPreferences.getInt("age", 1);
  Log.i(TAG, name + " : " +age);
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • android如何默認打開小區(qū)廣播具體實現(xiàn)

    android如何默認打開小區(qū)廣播具體實現(xiàn)

    小區(qū)廣播的開關,1是打開,0是關閉;0x00就默認關閉,改成0x01就是默認打開,具體修改如下,感興趣的朋友可以參考下哈
    2013-06-06
  • Android入門教程之Vibrator(振動器)

    Android入門教程之Vibrator(振動器)

    本節(jié)我們介紹的是Vibrator(振動器),是手機自帶的振動器,其實就是Android給我們提供的用于機身震動的一個服務!當收到推送消息的時候我們可以設置震動提醒。
    2016-07-07
  • Android檢測url地址是否可達的兩種方法

    Android檢測url地址是否可達的兩種方法

    今天小編就為大家分享一篇Android檢測url地址是否可達的兩種方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Android下拉列表spinner的實例代碼

    Android下拉列表spinner的實例代碼

    這篇文章主要為大家詳細介紹了Android下拉列表spinner的實例代碼。感興趣的小伙伴們可以參考一下
    2016-05-05
  • Android高效安全加載圖片的方法詳解

    Android高效安全加載圖片的方法詳解

    Android開發(fā)中消耗內(nèi)存較多一般都是在圖像上面,下面這篇文章主要給大家介紹了關于Android如何高效安全加載圖片的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-02-02
  • android開發(fā)教程之view組件添加邊框示例

    android開發(fā)教程之view組件添加邊框示例

    這篇文章主要介紹了android開發(fā)中給view組件添加邊框的示例,需要的朋友可以參考下
    2014-02-02
  • Android 中SQLite技術實例詳解

    Android 中SQLite技術實例詳解

    這篇文章主要介紹了Android 中SQLite技術實例詳解的相關資料,需要的朋友可以參考下
    2017-06-06
  • Android自定義 WebView瀏覽器

    Android自定義 WebView瀏覽器

    WebView是Android中一個非常實用的組件,它和Safai、Chrome一樣都是基于Webkit網(wǎng)頁渲染引擎。接下來通過本文給大家介紹android自定義webview瀏覽器,感興趣的朋友一起學習吧
    2016-05-05
  • 談談對Android View事件分發(fā)機制的理解

    談談對Android View事件分發(fā)機制的理解

    本篇文章主要介紹了談談對Android View事件分發(fā)機制的理解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • Android開發(fā)使用strings.xml多語言翻譯解決方案

    Android開發(fā)使用strings.xml多語言翻譯解決方案

    這篇文章主要為大家介紹了Android開發(fā)使用strings.xml多語言翻譯解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-06-06

最新評論

宝丰县| 南安市| 铁力市| 柏乡县| 同仁县| 天峨县| 兴宁市| 玉龙| 沅江市| 靖江市| 高要市| 沧源| 龙江县| 土默特左旗| 治多县| 临泽县| 安多县| 康定县| 芦溪县| 淅川县| 凤翔县| 民和| 炎陵县| 东兴市| 阜康市| 平定县| 云南省| 萍乡市| 油尖旺区| 天峨县| 边坝县| 博爱县| 阜康市| 丰顺县| 花莲市| 庆元县| 大理市| 卢氏县| 兴海县| 教育| 文安县|