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()*/
        }
    }

      查看方法和上述相同,记得刷新。(有些字符串我写在String.xml)

    结果:

      

  • 相关阅读:
    继承项目第13周项目1基类中成员的访问限定符和派生类的继承方式
    架构业务wait和sleep区别狭义jiavaBean规范,三层架构模式
    文件文本编辑器ASP.net使用CKEditor(html文本编辑器)
    彩信对象android(5)_发彩信操作
    方法接口UML统一建模语言,java中七种设计原则,
    jquery实现jQuery实现图片轮播效果,jQuery实现焦点新闻
    设备文件BSP及嵌入式驱动开发笔记
    Invalidate
    C#集合类使用范例
    判断一段程序是由C 编译程序还是由C++编译程序编译的
  • 原文地址:https://www.cnblogs.com/yangxiao-/p/12251637.html
Copyright © 2011-2022 走看看