android 如何保存简单的配置信息_android安装及配置
android 如何保存简单的配置信息由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“android安装及配置”。
我們知道在android的開發中,保存項目私有數據的存儲方式我們可以使用:SharedPreferences,File,SQLite,Network.四種方式,而要用到應用程序之間數據的共享要使用ContentProvider。那今天我們只敘述一下僅僅保存一些我們登錄等的一些配置信息的數據,也就是說用到的數據量都不是很大,那麼我們就可以選擇SharedPreferences和File的方式。這裡只針對性的結合File和Properties進行敘述。
一。SharedPreferences
1.它可以保存上一次用戶所做的修改或者自定義參數的設定,當再次啟動程序後依然可以保持原有的設置。這裡只說明一下使用方式。比如下面的代碼在OnCreate中使用:
SharedPreferences mSharedPreferences = getSharedPreferences(“list”,MODE_PRIVATE);
String mTempString = mSharedPreferences.getString(“config”,“default”);其中“list”是SharedPreferences的文件的名字,SharedPreferences是以鍵值映射的關係存放數據。不過多解釋,你也可以這樣用:
SharedPreferences mSharedPreferences = getPreferences(MODE_PRIVATE);這樣默認的文件名是activity的名字。
2.退出activity的時候保存數據,在OnPause中使用:
SharedPreferences mSharedPreferences = getSharedPreferences(“list”,MODE_PRIVATE);
mSharedPreferences.edit().putString(“config”,“data”).commit();
3.SharedPreferences 是以xml文件的方式自動保存的,在DDMS中的FileExplorer中展開/data/data/包名/shared-prefs下面就是SharedPreferences文件。
4.SharedPreferences文件只可以用來存放基本的數據類型。
二。結合File和Properties進行保存。
A Properties object is a Hashtable where the keys and values must be Strings.Each property can have a default Properties list which specifies the default values to be used when a given key is not found in this Properties instance.1.所以,Properties對象也是一個哈希表,也是一個鍵值對應的關係,因此和上面的操作相似。下面看具體的程序。
public cla File_ByProperties extends Activity {
/** Called when the activity is first created.*/ @Override public void onCreate(Bundle savedInstanceState){ private boolean mStatus;private TextView mShowStatus;
} setContentView(R.layout.main);mShowStatus =(TextView)findViewById(R.id.show);load();private void load(){// TODO Auto-generated method stub Properties mProperties = new Properties();try {FileInputStream mInputStream =
openFileInput(“configuration”);
mProperties.load(mInputStream);mStatus =
Boolean.valueOf(mProperties.get(“status”).toString());
@Override protected void onPause(){// TODO Auto-generated method stub super.onPause();@Override public boolean onKeyDown(int keyCode, KeyEvent event){} // TODO Auto-generated method stub if(keyCode == KeyEvent.KEYCODE_DPAD_UP){} return super.onKeyDown(keyCode, event);mStatus =!mStatus;mShowStatus.setText(“the status is : ” + mStatus);}mShowStatus.setText(“the status is : ” + mStatus);} catch(FileNotFoundException e){// TODO Auto-generated catch block System.out.println(e.toString());} catch(IOException e){} System.out.println(e.toString());
if(mProperties.containsKey(“status”)){} mProperties.put(“status”, String.valueOf(mStatus));try {FileOutputStream mOutputStream = mProperties.remove(“status”);
openFileOutput(“configuration”,}
2.在DDMS中的FileExplorer中展開/data/data/包名/files,可以查看到該文件。
三。你還可以將一個靜態的文件放到res/raw/下面,然後通過
getResources().openRawResource(R.raw.文件);來得到一個InputStream對象,然後讀取文件的內容。
轉載請尊重原創,這裡是的空間。}MODE_WORLD_WRITEABLE);mProperties.store(mOutputStream, null);} catch(FileNotFoundException e){// TODO Auto-generated catch block e.printStackTrace();} catch(IOException e){} e.printStackTrace();