zoukankan      html  css  js  c++  java
  • android----SharedPreference永久保存数据

    SharedPreference是一种轻量级的内部数据存储方式,采用Key/value的形式,只运行存储一些简单的数据,比如int,float等类型

      首先先创建SharedPreference

    复制代码
    package com.example.sharedpreference;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // SharedPreferences是一个接口,通过get方法获取这个接口
            SharedPreferences shp = getPreferences(Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = shp.edit();
            // 存储(key-value)
            editor.putInt("number", 100);
            // 提交
            editor.apply();  // apply可以有效的避免冲突
    
           // 或者用提交,建议用apply editor.commit()
        }
    }
    复制代码

          点击(右下角),选择data,在选择data,查找包名,就会看到创建的SharedPreference(如果没有就刷新一下),

    这是系统默认给我们创建的(因为我们用的是getPreferences的第一个方法,不运行其它的activity访问。)点击该XML文件就会看到我们存储的数据。

      如果我们用第二个getPreferences的方法,那么其它的activity就会访问它。构造方法类似,按上述查找方法查找。

      

    复制代码
    package com.example.sharedpreference;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // SharedPreferences是一个接口,通过get方法获取这个接口
            SharedPreferences shp = getSharedPreferences("MY_DATA", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = shp.edit();
            // 存储(key-value)
            editor.putInt("NUMBER", 700);
            // 提交
            editor.apply();  // apply可以有效的避免冲突
    
           // 或者用提交,建议用apply editor.commit()
        }
    }
    复制代码

      

      点击自己创建的xml文件就会看到存储的数字。

      之所以可以用getSharedPreferences是因为该方法是在ContextWrapper里,而ContextWrapper是继承Context。

         上述两种方法都是在activity内部访问的,如果是在activity外部访问,则需要传递一个Context。

        

      外部类:MyData

    复制代码
    package com.example.sharedpreference;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    
    public class MyData {
        public int number;
        public Context context;
        // 构造函数,接受传来的Context
        public MyData(Context context){
            this.context = context;
        }
        // 存储
        public void save(){
            String name = context.getResources().getString(R.string.MY_DATA);
            SharedPreferences shp = context.getSharedPreferences(name,Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = shp.edit();
            String key = context.getResources().getString(R.string.MY_KEY);
            // 把number存入
            editor.putInt(key,number);
            // 提交
            editor.apply();
        }
    }
    复制代码

      Activity:

    复制代码
    package com.example.sharedpreference;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // 不要传this,this是对Activity的引用,而在程序运行中Activity经常被摧毁、重新创建,会造成内存泄漏
    
            MyData myData = new MyData(getApplicationContext());//getApplicationContext()的生命周期是伴随整个app的
            // 复制
            myData.number = 1000;
            // 存储
            myData.save();
    
    
           /* // SharedPreferences是一个接口,通过get方法获取这个接口
            SharedPreferences shp = getSharedPreferences("MY_DATA", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = shp.edit();
            // 存储(key-value)
            editor.putInt("NUMBER", 700);
            // 提交
            editor.apply();  // apply可以有效的避免冲突
    
           // 或者用提交,建议用apply editor.commit()*/
        }
    }
    复制代码
  • 相关阅读:
    004 eclipse-jee-2021-06-R-win32-x86_64的使用
    男神鹏:Mac系统下查看和生成SSH Key
    每天一个Python小技巧(2)
    每天一个Python小技巧(1)之JSON转义
    测试平台系列(26) 编写用例详情页(1)
    测试平台系列(25) 编写用例树
    iOS 骨架屏
    OC 拖拽View
    OC 配置全局PCH 文件
    iOS masonary 约束 做动画
  • 原文地址:https://www.cnblogs.com/hrzgj/p/14850468.html
Copyright © 2011-2022 走看看