zoukankan      html  css  js  c++  java
  • 安卓基础(永久保存数据)

    今天学习了永久保存数据这一块的知识,利用viewmodel的知识可以暂时储存数据,但是当app后台被清除的时候,数据就会丢失,但是利用sharedpreferences的知识就可以做到将数据保存

    下面是我做的一个实例:

    MainAcitivity:(注释部分是最简单的本类中储存)

    package com.example.sharedpreferences;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    
    public class MainActivity extends AppCompatActivity {
    String TAG = "key";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
                 MyData myData = new MyData(getApplicationContext());
                 myData.number=1000;
                 myData.save();
                 int y =myData.load();
                 Log.d(TAG, String.valueOf(y));
            //       // SharedPreferences shp = getPreferences(Context.MODE_PRIVATE);
    //        SharedPreferences shp = getSharedPreferences("MY_DATA",Context.MODE_PRIVATE);
    //        SharedPreferences.Editor  editor = shp.edit();
    //        editor.putInt("NUMBER",500);
    //        editor.apply();
    //        int x = shp.getInt("NUMBER",0);
    //        Log.d(TAG, String.valueOf(x));
        }
    }

    MyData:

    package com.example.sharedpreferences;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    
    import java.security.PublicKey;
    
    public class MyData {
        public int number;
        private 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.KEY);
            editor.putInt("key",number);
            editor.apply();
        }
        public int load(){
    String  name = context.getResources().getString(R.string.MY_DATA);
    SharedPreferences shp = context.getSharedPreferences(name,Context.MODE_PRIVATE);
    String key = context.getResources().getString(R.string.KEY);
    int x = shp.getInt(key,0);
    return x;
    }

     点开androidstudio的右下角的DECice Experiencorer在点击data目录/data目录

    再再里面找到自己项目的目录

     打开xml文件就可以看到自己储存的数据

  • 相关阅读:
    Intent
    What should we do next in general after collecting relevant data
    NOTE FOR Secure Friend Discovery in Mobile Social Networks
    missing pcap.h
    after building Android Source code
    plot point(one column)
    When talking to someone else, don't infer that is has been talked with others at first. It may bring repulsion to the person who is talking with you.
    进程基本知识
    Python input和raw_input的区别
    强制 code review:reviewboard+svn 的方案
  • 原文地址:https://www.cnblogs.com/yangxionghao/p/12300891.html
Copyright © 2011-2022 走看看