分享

SharedPreferences的存储位置和格式

 流浪的星星318 2017-03-25

1.SharedPreferences 本身是一个接口,程序无法直接创建SharedPreferences实例,只能通过Context提供的getSharedPreferences(String name,int mode)来获取该实例

该方法的第二个参数可以设置模式有:

(1).Context.MODE_PRIVATE:指定该SharedPreferences数据只能被应用程序读写

(2).Context.MODE_WORLD_READABLE:指定该SharedPreferences数据能被其他应用程序读,但不能写。

(3).Context,MODE_WORLD_WRITEABLE:指定该SharedPreferences数据能被其他应用程序读写。

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

  1. public class LoginActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {  
  2.   
  3.     // SharedPreferences 使用规则:  
  4.     // 1. 存储方式:保存到文件中  
  5.     // 2. 存储格式: Key-Value  
  6.     // 3. 存储内容: 不要过长,精简的  
  7.     // 4. 应用场景: 配置字段、用户信息、  
  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 在使用的时候,应该先看一下是否保存过数据  
  23.   
  24.   
  25.         SharedPreferences sp =  
  26.                 getSharedPreferences("app", MODE_PRIVATE);  
  27.   
  28.         // 3.监测是否有记住密码的功能,同时设置 CheckBox的状态变化  
  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.         // 添加内容到存储区  
  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存储:Context提供了两个方法来打开应用程序的数据文件里的文件IO流

(1).FileInputStream openFileInput(String name): 打开应用程序的数据文件夹下的name文件对应的输入流

(2).FileOutputStream openFileOutput(String name,int mode):打开应用程序的数据文件夹下的那么文件对应的输出流

<1>MODE_PRIVATE: 该文件只能被当前程序读写

<2>MODE_APPEND:以追加的方式打开该文件,应用程序可以向该文件中追加内容。

<3>MODE_WORLD_READABLE:该文件的内容可以被其他程序读取

<4>MODE_WORLD_WRITEABLE:该文件的内容可以被其他程序读写

android 4.2开始<3>、<4>不再推荐使用

3.读写SD卡上的文件

(1).Environment.getExternalStorageState();// 1. 监测当前手机是否包含存储卡, 所有外部存储的操作都需要在清单文件声明权限
        (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. 监测当前手机是否包含存储卡,  
  13.         //    所有外部存储的操作都需要在清单文件声明权限  
  14.         String state = Environment.getExternalStorageState();  
  15.         // 根据状态判断是否有外部存储  
  16.         if (Environment.MEDIA_MOUNTED.equals(state)) {  
  17.             // 外部存储已经挂载,可以访问和使用  
  18.   
  19.             // 2. 获取外部存储的根目录  
  20.             File directory = Environment.getExternalStorageDirectory();  
  21.   
  22.             Log.d("ExternalActivity", "外部存储目录 " + directory.getAbsolutePath());  
  23.   
  24.   
  25.             // 3. 获取外部存储,公共目录  
  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. 外部存储可以获取 应用程序特定的一些目录,类似于内部存储的路劲  
  39.             //    /外部根目录/Android/data/包名/  
  40.             //    使用上下文 Context来获取  
  41.   
  42.             getExternalCacheDir();  
  43.   
  44.             // 获取外部存储区中,应用程序自身 files 目录内部的文件夹  
  45.             // 如果传递的参数为 null,直接返回 files 目录,  
  46.             // 否则  files/xxxx  目录  
  47.             getExternalFilesDir("images");  
  48.         }  
  49.     }  
  50. }  


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多