Android数据存储SharedPreferences的使用场景和注意事项

Shared Preferences:
       是一种轻量级的存储,数据会写在本地的一个xml文件中,以键值对的形式存在!如果程序卸载,此文件也跟着卸载!!
       保存位置:  
                  data/data/程序包名/share_prefs/
      主要用途:
                 1.保存应用的设置  例如:设置静音,下次进入还是静音
                 2.判断是否是第一次登陆
      使用步骤:
                mode_privatereadable  writeable  writeable
                ShareedPreference sharedPreferences = getSharedPreferencesMODE_PRIVATE
                SharedPreferences.Editor = sharedPreferences.edit();
                写入:
                获取sharedPreference:SharedPreferences sharedPreferences = getSharedPreferences(name,mode);
                创建画笔:Editor edit = sharedPreferences.edit();
                写数据
                eg:edit.putString(“name”, “金三胖”);
                     edit.putInt(“age”, 36);
                     edit.putBoolean(“hasXF”, true);
               提交数据

               edit.commit();

     注意事项:

               配置文件,强烈建议,要写成私有模式
              SharedPreferences sharedPreferences = getSharedPreferences(name,MODE_PRIVATE);

Java代码详见如下:(这里加入了一个上下文菜单的效果)

public class MainActivity extends AppCompatActivity {
   private TextView textView;
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == 0) {
                // 跟新文本内容
                textView.setText(msg.arg1 + "");
            } else if (msg.what == 1) {
                // 进行页面跳转
                Intent intent = new Intent();
                ComponentName componentName = new ComponentName(
                        MainActivity.this, SecondActivity.class);
                intent.setComponent(componentName);
                startActivity(intent);
                finish();
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.iv_01);
        // 是否是第一次登陆
        SharedPreferences sharedPreferences = getSharedPreferences("isOne", MODE_PRIVATE);
        boolean isOne = sharedPreferences.getBoolean("is", false);
        if (isOne) {
            // 登陆过
            Intent intent = new Intent();
            ComponentName componentName = new ComponentName(MainActivity.this,
                    SecondActivity.class);
            intent.setComponent(componentName);
            startActivity(intent);
            finish();
        } else {
            // 没有登陆过  停留5秒 跳转到下一页面
            Sleep sleep = new Sleep();
            Thread thread = new Thread(sleep);
            thread.start();
        }
    }
    //实现接口的写法
    class Sleep implements Runnable{
    @Override
    public void run() {
        for (int i = 5; i > 0; i--) {
                Message msg = new Message();
                msg.what = 0;
                msg.arg1 = i;
                handler.sendMessage(msg);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            // 告诉主线程,进行页面跳
            Message message = new Message();
            message.what = 1;
            handler.sendMessage(message);
     }
   }
}

一个简单的Demo演示SharedPreferences的练习.

    原文作者:csdn_OF_ZHAO
    原文地址: https://blog.csdn.net/CSDN_OF_ZHAO/article/details/52108017
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞