Android如何实现关机后数据不会丢失问题

  介绍

小编给大家分享一下安卓如何实现关机后数据不会丢失问题,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获、下面让我们一起去了解一下吧!

要实现关机后数据也不会丢失,需要使用到AndroidViewModel, SaveStateHandle和SharePreferences要达到的目的就是将数据保存成这个亚子

 Android如何实现关机后数据不会丢失问题

就不会出现应用在异常闪退或者关机后数据的丢失了注意在使用SaveStateHandle和绑定的时候需要在gradle里面设置一波

 Android如何实现关机后数据不会丢失问题

数据类

package  com.example.applicationtest04;      import  android.app.Application;   import  android.content.Context;   import  android.content.SharedPreferences;      import  androidx.annotation.NonNull;   import  androidx.lifecycle.AndroidViewModel;   import  androidx.lifecycle.LiveData;   import  androidx.lifecycle.MutableLiveData;   import  androidx.lifecycle.SavedStateHandle;      public  class  MyVIewModel  extends  AndroidViewModel  {   ,SavedStateHandle 处理;//声明SavedStateHandle 类型   ,String  shpName =, getApplication () .getResources () .getString (R.string.shp_name);   ,String  key =, getApplication () .getResources () .getString (R.string.key);   ,public  MyVIewModel (@NonNull  Application 应用程序,,SavedStateHandle 处理),{   超级才能(应用程序);   时间=this.handle 才能;处理;   如果才能(! handle.contains(关键)){   ,,负载();   ,,}   ,}   ,public  LiveData getNumber () {   return 才能handle.getLiveData(关键);   ,}   ,public  void 负载(){   SharedPreferences 才能;shp =, getApplication () .getSharedPreferences (Context.MODE_PRIVATE shpName也);   int 才能;x =, shp.getInt(键,0);   handle.set才能(关键,x);      ,}   ,public  void 保存(){   SharedPreferences 才能;shp =, getApplication () .getSharedPreferences (shpName Context.MODE_PRIVATE);   SharedPreferences.Editor 才能;editor =, shp.edit ();   editor.putInt才能(关键getNumber () .getValue ());   editor.apply才能();   ,}   ,public  void 添加(int  x) {   handle.set才能(关键getNumber () .getValue () + x);   ,}   }//这段代码里面有几个重要的点就是在使用处理的时候要注意使用的数据是liveData

Mainactive类

package  com.example.applicationtest04;      import  androidx.appcompat.app.AppCompatActivity;   import  androidx.databinding.DataBindingUtil;   import  androidx.lifecycle.SavedStateVMFactory;   import  androidx.lifecycle.ViewModelProvider;   import  androidx.lifecycle.ViewModelProviders;      import  android.os.Bundle;      import  com.example.applicationtest04.databinding.ActivityMainBinding;      public  class  MainActivity  extends  AppCompatActivity  {   ,MyVIewModel  myVIewModel;   ,ActivityMainBinding 绑定;   ,@Override   ,protected  void  onCreate (Bundle  savedInstanceState), {   super.onCreate才能(savedInstanceState);   时间=binding 才能;DataBindingUtil.setContentView(这个,R.layout.activity_main);   this.myVIewModel 才能=,ViewModelProviders.of(这个,new  SavedStateVMFactory(这)). get (MyVIewModel.class);   binding.setData才能(myVIewModel);   binding.setLifecycleOwner才能(这个);   ,}      ,@Override   ,protected  void  onPause (), {   super.onPause才能();   myVIewModel.save才能();   ,}   }//这段代码的重点就是使用onPause这个声明周期的函数来调用保存()函数

布局xml

& lt; ? xml  version=?.0“,编码=皍tf-8" ?比;   & lt; layout  xmlns: android=癶ttp://schemas.android.com/apk/res/android"   ,xmlns:应用=癶ttp://schemas.android.com/apk/res-auto"   ,xmlns:工具=癶ttp://schemas.android.com/tools"比;   ,& lt; data>   & lt;才能变量   ,,name=癉ata"   ,,类型=癱om.example.applicationtest04.MyVIewModel",/比;   ,& lt;/data>   & lt; androidx.constraintlayout.widget.ConstraintLayout   android:才能layout_width=癿atch_parent"   android:才能layout_height=癿atch_parent"   工具:才能上下文=?MainActivity"祝辞   & lt; TextView才能   ,,android: layout_width=皐rap_content"   ,,android: layout_height=皐rap_content"   ,,android:文本=癅 {String.valueOf (Data.getNumber ())},   ,,android:输入textColor=癅color/colorPrimaryDark"   ,,android: textSize=?6 sp"   ,才能应用:layout_constraintBottom_toBottomOf=皃arent"   ,才能应用:layout_constraintHorizontal_bias=?.497”;   ,才能应用:layout_constraintLeft_toLeftOf=皃arent"   ,才能应用:layout_constraintRight_toRightOf=皃arent"   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

Android如何实现关机后数据不会丢失问题