当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现关机后数据不会丢失问题

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

2020年03月09日  | 移动技术网IT编程  | 我要评论

营养分析,这就是中锋,2013年贷款利率

要实现关机后数据也不会丢失,需要使用到 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实现关机后数据不会丢失问题,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网