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

 更新时间:2019年10月28日 15:19:06   作者:毛少雄  
这篇文章主要介绍了Android实现关机后数据不会丢失问题,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

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

在这里插入图片描述

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

在这里插入图片描述

数据类

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 handle; //声明savedstatehandle 类型
 String shpName = getApplication().getResources().getString(R.string.shp_name);
 String key = getApplication().getResources().getString(R.string.key);
 public MyVIewModel(@NonNull Application application, SavedStateHandle handle) {
  super(application);
  this.handle = handle;
  if(!handle.contains(key)){
   load();
  }
 }
 public LiveData<Integer> getNumber(){
  return handle.getLiveData(key);
 }
 public void load(){
  SharedPreferences shp = getApplication().getSharedPreferences(shpName, Context.MODE_PRIVATE);
  int x = shp.getInt(key,0);
  handle.set(key,x);

 }
 public void save(){
  SharedPreferences shp = getApplication().getSharedPreferences(shpName,Context.MODE_PRIVATE);
  SharedPreferences.Editor editor = shp.edit();
  editor.putInt(key,getNumber().getValue());
  editor.apply();
 }
 public void add(int x){
  handle.set(key,getNumber().getValue()+x);
 }
}
//这段代码里面有几个重要的点就是在使用handle的时候要注意使用的数据是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 binding;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
  this.myVIewModel = ViewModelProviders.of(this,new SavedStateVMFactory(this)).get(MyVIewModel.class);
  binding.setData(myVIewModel);
  binding.setLifecycleOwner(this);
 }

 @Override
 protected void onPause() {
  super.onPause();
  myVIewModel.save();
 }
}
//这段代码的重点就是使用onPause这个声明周期的函数来调用save()函数

布局xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools">
 <data>
  <variable
   name="Data"
   type="com.example.applicationtest04.MyVIewModel" />
 </data>
 <androidx.constraintlayout.widget.ConstraintLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity">
  <TextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@{String.valueOf(Data.getNumber())}"
   android:textColor="@color/colorPrimaryDark"
   android:textSize="36sp"
   app:layout_constraintBottom_toBottomOf="parent"
   app:layout_constraintHorizontal_bias="0.497"
   app:layout_constraintLeft_toLeftOf="parent"
   app:layout_constraintRight_toRightOf="parent"
   app:layout_constraintTop_toTopOf="parent"
   app:layout_constraintVertical_bias="0.324" />
  <Button
   android:id="@+id/button"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginStart="8dp"
   android:layout_marginTop="8dp"
   android:layout_marginEnd="8dp"
   android:layout_marginBottom="8dp"
   android:text="@string/buttonPlus"
   android:onClick="@{()->Data.add(1)}"
   app:layout_constraintBottom_toBottomOf="parent"
   app:layout_constraintEnd_toEndOf="parent"
   app:layout_constraintHorizontal_bias="0.182"
   app:layout_constraintStart_toStartOf="parent"
   app:layout_constraintTop_toTopOf="parent"
   app:layout_constraintVertical_bias="0.499" />
  <Button
   android:id="@+id/button2"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_marginStart="8dp"
   android:layout_marginTop="8dp"
   android:layout_marginEnd="8dp"
   android:layout_marginBottom="8dp"
   android:text="@string/buttonSub"
   android:onClick="@{()->Data.add(-1)}"
   app:layout_constraintBottom_toBottomOf="parent"
   app:layout_constraintEnd_toEndOf="parent"
   app:layout_constraintHorizontal_bias="0.804"
   app:layout_constraintStart_toStartOf="parent"
   app:layout_constraintTop_toTopOf="parent"
   app:layout_constraintVertical_bias="0.499" />
 </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

测试效果先加到12

在这里插入图片描述

再重启

在这里插入图片描述

重启之后重新打开app

在这里插入图片描述

值还是没有变化测试成功

总结

以上所述是小编给大家介绍的Android实现关机后数据不会丢失问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

  • Android开发之使用通知栏显示提醒信息的方法

    Android开发之使用通知栏显示提醒信息的方法

    这篇文章主要介绍了Android开发之使用通知栏显示提醒信息的方法,涉及Notification的使用及通知栏信息设置技巧,需要的朋友可以参考下
    2016-01-01
  • Android6.0获取GPS定位和获取位置权限和位置信息的方法

    Android6.0获取GPS定位和获取位置权限和位置信息的方法

    今天小编就为大家分享一篇Android6.0获取GPS定位和获取位置权限和位置信息的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • android getActivity.findViewById获取ListView 返回NULL的方法

    android getActivity.findViewById获取ListView 返回NULL的方法

    下面小编就为大家带来一篇android getActivity.findViewById获取ListView 返回NULL的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-11-11
  • Android开发中setContentView和inflate的区别分析

    Android开发中setContentView和inflate的区别分析

    这篇文章主要介绍了Android开发中setContentView和inflate的区别,较为详细的分析了setContentView和inflate的功能、用法及二者的区别,需要的朋友可以参考下
    2016-07-07
  • Android实现翻页特效

    Android实现翻页特效

    这篇文章主要为大家详细介绍了Android实现翻页特效,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • Android 滑动监听的实例详解

    Android 滑动监听的实例详解

    这篇文章主要介绍了Android 滑动监听的实例详解的相关资料,希望通过本能帮助到大家,需要的朋友可以参考下
    2017-09-09
  • SharedPreference引发ANR原理详解

    SharedPreference引发ANR原理详解

    这篇文章主要为大家介绍了SharedPreference引发ANR原理详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • Android RecyclerView实现滑动删除

    Android RecyclerView实现滑动删除

    这篇文章主要为大家详细介绍了Android RecyclerView实现滑动删除,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-07-07
  • Android编程实现打勾显示输入密码功能

    Android编程实现打勾显示输入密码功能

    这篇文章主要介绍了Android编程实现打勾显示输入密码功能,涉及Android控件布局及属性相关操作技巧,需要的朋友可以参考下
    2017-02-02
  • Flutter开发之Widget自定义总结

    Flutter开发之Widget自定义总结

    这篇文章主要给大家介绍了关于Flutter开发中Widget自定义的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Flutter具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-04-04

最新评论