小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

SharedPreferences的存儲(chǔ)位置和格式

 流浪的星星318 2017-03-25

1.SharedPreferences 本身是一個(gè)接口,程序無法直接創(chuàng)建SharedPreferences實(shí)例,只能通過Context提供的getSharedPreferences(String name,int mode)來獲取該實(shí)例

該方法的第二個(gè)參數(shù)可以設(shè)置模式有:

(1).Context.MODE_PRIVATE:指定該SharedPreferences數(shù)據(jù)只能被應(yīng)用程序讀寫

(2).Context.MODE_WORLD_READABLE:指定該SharedPreferences數(shù)據(jù)能被其他應(yīng)用程序讀,但不能寫。

(3).Context,MODE_WORLD_WRITEABLE:指定該SharedPreferences數(shù)據(jù)能被其他應(yīng)用程序讀寫。

Android 4.2 模式(2)、(3)就不再推薦使用。

  1. public class LoginActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {  
  2.   
  3.     // SharedPreferences 使用規(guī)則:  
  4.     // 1. 存儲(chǔ)方式:保存到文件中  
  5.     // 2. 存儲(chǔ)格式: Key-Value  
  6.     // 3. 存儲(chǔ)內(nèi)容: 不要過長(zhǎng),精簡(jiǎn)的  
  7.     // 4. 應(yīng)用場(chǎng)景: 配置字段、用戶信息、  
  8.   
  9.     private EditText mTxtName;  
  10.   
  11.     private EditText mTxtPass;  
  12.   
  13.     @Override  
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_login);  
  17.   
  18.         mTxtName = (EditText) findViewById(R.id.txt_name);  
  19.         mTxtPass = (EditText) findViewById(R.id.txt_pass);  
  20.   
  21.   
  22.         // 1. SharedPreferences 在使用的時(shí)候,應(yīng)該先看一下是否保存過數(shù)據(jù)  
  23.   
  24.   
  25.         SharedPreferences sp =  
  26.                 getSharedPreferences("app", MODE_PRIVATE);  
  27.   
  28.         // 3.監(jiān)測(cè)是否有記住密碼的功能,同時(shí)設(shè)置 CheckBox的狀態(tài)變化  
  29.         CheckBox chbRememberPass = (CheckBox) findViewById(R.id.chb_remember_pass);  
  30.   
  31.         boolean rp = sp.getBoolean("rememberPass", false);  
  32.         chbRememberPass.setChecked(rp);  
  33.   
  34.         chbRememberPass.setOnCheckedChangeListener(this);  
  35.   
  36.         if(rp) {  
  37.             // 2. 使用 getXxxx(String key, ..defaultValue)  
  38.             String name = sp.getString("name", null);  
  39.             String pass = sp.getString("pass", null);  
  40.   
  41.             mTxtName.setText(name);  
  42.   
  43.             mTxtPass.setText(pass);  
  44.         }  
  45.   
  46.     }  
  47.   
  48.   
  49.     public void btnLogin(View view) {  
  50.         SharedPreferences sp = getSharedPreferences("app", MODE_PRIVATE);  
  51.         // 保存配置到 SharedPreferences  
  52.         SharedPreferences.Editor editor = sp.edit();  
  53.   
  54.         // 添加內(nèi)容到存儲(chǔ)區(qū)  
  55.         editor.putString("name", mTxtName.getText().toString());  
  56.   
  57.         editor.putString("pass", mTxtPass.getText().toString());  
  58.   
  59.         // Editor 必須要 提交 可以使用commit() 或者 apply() (API 9以上)  
  60.   
  61.         editor.apply();  
  62.   
  63.         Intent intent = new Intent(this, MainActivity.class);  
  64.         startActivity(intent);  
  65.   
  66.     }  
  67.   
  68.     @Override  
  69.     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  
  70.   
  71.         SharedPreferences sp = getSharedPreferences("app", MODE_PRIVATE);  
  72.   
  73.         SharedPreferences.Editor editor = sp.edit();  
  74.   
  75.         if(isChecked){  
  76.             editor.putBoolean("rememberPass", true);  
  77.         }else{  
  78.             editor.remove("rememberPass");  
  79.         }  
  80.   
  81.         editor.apply();  
  82.   
  83.     }  
  84. }  
2.android的File存儲(chǔ):Context提供了兩個(gè)方法來打開應(yīng)用程序的數(shù)據(jù)文件里的文件IO流

(1).FileInputStream openFileInput(String name): 打開應(yīng)用程序的數(shù)據(jù)文件夾下的name文件對(duì)應(yīng)的輸入流

(2).FileOutputStream openFileOutput(String name,int mode):打開應(yīng)用程序的數(shù)據(jù)文件夾下的那么文件對(duì)應(yīng)的輸出流

<1>MODE_PRIVATE: 該文件只能被當(dāng)前程序讀寫

<2>MODE_APPEND:以追加的方式打開該文件,應(yīng)用程序可以向該文件中追加內(nèi)容。

<3>MODE_WORLD_READABLE:該文件的內(nèi)容可以被其他程序讀取

<4>MODE_WORLD_WRITEABLE:該文件的內(nèi)容可以被其他程序讀寫

android 4.2開始<3>、<4>不再推薦使用

3.讀寫SD卡上的文件

(1).Environment.getExternalStorageState();// 1. 監(jiān)測(cè)當(dāng)前手機(jī)是否包含存儲(chǔ)卡, 所有外部存儲(chǔ)的操作都需要在清單文件聲明權(quán)限
        (2).Environment.getExternalStorageDirectory(); 獲取SD卡的目錄  

  1. public class ExternalActivity extends AppCompatActivity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_external);  
  7.   
  8.         testSdcard();  
  9.     }  
  10.   
  11.     public void testSdcard() {  
  12.         // 1. 監(jiān)測(cè)當(dāng)前手機(jī)是否包含存儲(chǔ)卡,  
  13.         //    所有外部存儲(chǔ)的操作都需要在清單文件聲明權(quán)限  
  14.         String state = Environment.getExternalStorageState();  
  15.         // 根據(jù)狀態(tài)判斷是否有外部存儲(chǔ)  
  16.         if (Environment.MEDIA_MOUNTED.equals(state)) {  
  17.             // 外部存儲(chǔ)已經(jīng)掛載,可以訪問和使用  
  18.   
  19.             // 2. 獲取外部存儲(chǔ)的根目錄  
  20.             File directory = Environment.getExternalStorageDirectory();  
  21.   
  22.             Log.d("ExternalActivity", "外部存儲(chǔ)目錄 " + directory.getAbsolutePath());  
  23.   
  24.   
  25.             // 3. 獲取外部存儲(chǔ),公共目錄  
  26.             File dcimDir = Environment.getExternalStoragePublicDirectory(  
  27.                     Environment.DIRECTORY_DCIM  
  28.             );  
  29.   
  30.             if (dcimDir.exists()) {  
  31.                 // TODO: 遍歷所有的文件,打印出來  
  32.                 File[] files = dcimDir.listFiles();  
  33.                 for (File file : files) {  
  34.                     Log.d("ExternalActivity", "file = " + file);  
  35.                 }  
  36.             }  
  37.   
  38.             // 4. 外部存儲(chǔ)可以獲取 應(yīng)用程序特定的一些目錄,類似于內(nèi)部存儲(chǔ)的路勁  
  39.             //    /外部根目錄/Android/data/包名/  
  40.             //    使用上下文 Context來獲取  
  41.   
  42.             getExternalCacheDir();  
  43.   
  44.             // 獲取外部存儲(chǔ)區(qū)中,應(yīng)用程序自身 files 目錄內(nèi)部的文件夾  
  45.             // 如果傳遞的參數(shù)為 null,直接返回 files 目錄,  
  46.             // 否則  files/xxxx  目錄  
  47.             getExternalFilesDir("images");  
  48.         }  
  49.     }  
  50. }  


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多