当前位置: 首页 > 编程笔记 >

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

查修谨
2023-03-14
本文向大家介绍Android实现关机后数据不会丢失问题,包括了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实现关机后数据不会丢失问题,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对小牛知识库网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

 类似资料:
  • 这篇文章,给不太熟悉MQ技术的同学,介绍一个生产环境中可能会遇到的问题。 目前为止,你的RabbitMQ部署在线上服务器了,对吧?然后订单服务和仓储服务都可以基于RabbitMQ来收发消息,同时仓储服务宕机,不会导致消息丢失。 好,我们来看下目前为止的架构图。 那如果此时出现一个问题,就是说订单服务投递了订单消息到RabbitMQ里去,RabbitMQ暂时放在了自己的内存中,还没来得及投递给下游的

  • 问题内容: 我正在使用SQL数据库,我有一列名为“价格”。创建数据库后,将“价格”列设置为“我”,需要将其类型更改为不丢失数据库中的数据。这应该通过SQL脚本来完成 我想到了创建一个新列,将数据移到其中,删除旧列,然后重命名新创建的列。 有人可以帮我举个例子吗?在SQL中也有一个函数可以将字符串解析为十进制? 谢谢 问题答案: 您无需添加新列两次,只需在更新新列后删除旧列即可: 请注意,如果不是数

  • 在我们的应用程序中,用户多年来一直在使用以下代码上传数百万张图像: 最近,我们看到需要保存上传图像的数据。问题在于,压缩位图时图像Exif数据丢失。我考虑使用从原始文件中提取此数据: ..然后将其添加到InputStream 中,然后继续上传文件。问题是< code>ExifInterface无法将Exif数据保存到InputStream。 当Exif数据上传到服务器时,如何将它们保留在图像中?

  • 主要内容:一、背景引入,二、Kafka分布式存储架构,三、Kafka高可用架构,四、Kafka写入数据丢失问题,五、Kafka的ISR机制是什么?,六、数据如何保证不丢失?,七、总结一、背景引入 这篇文章,给大家聊一下写入Kafka的数据该如何保证其不丢失? 看过之前的文章《字节面试官: 让你设计一个MQ每秒要抗几十万并发,怎么做?》的同学,应该都知道写入Kafka的数据是会落地写入磁盘的。 我们暂且不考虑写磁盘的具体过程,先大致看看下面的图,这代表了Kafka的核心架构原理。 二、Kafka分

  • 问题内容: 我有一个与该线程中的问题相似的问题,即使不完全相同: 仅在GoogleChrome和URL重写中随机丢失会话变量 但是该线程中的所有解决方案都不适合我。我的PHP / MySQL应用程序中只有Google Chrome出现了奇怪的行为。如果我在Firefox上尝试过,它可以工作,但Chrome不能。 我导航到购物车中的某个位置,并在代码中的多个位置存储会话数据。不用担心我开始会议或与此

  • 问题内容: 如何解决在PHP中重定向后丢失会话的问题? 最近,我遇到了一个非常常见的问题,即在重定向后丢失会话。在搜索该网站后,我仍然找不到任何解决方案(尽管这是最接近的解决方案)。 更新资料 我找到了答案,我想将它发布在这里,以帮助遇到同样问题的任何人。 问题答案: 首先,执行以下常规检查: 确保在任何会话被调用之前被调用。因此,一个安全的选择就是将其放在页面的开头,紧接着在开始声明之后,再放在

  • 我对使用PrimeFaces 3.4.1和JSF 2.1.6的dataTable有以下问题。第一次加载页面时,数据表中充满了数据库中的数据,但每当在页面上执行操作(编辑链接、数据表排序或过滤)时,数据表就会丢失其数据。DataTable包含来自此类的元素: 在dataTable上执行操作后进行调试时,我们只在envioDatasujeto字段上保留数据,元素上的其他所有数据都会丢失。 这是xhtm

  • 我正在使用DSS签署Pdf文档。我需要这些文档有时间戳并启用LTV(启用PAdES LTV)。 我遇到了一些关于撤销数据的问题。 我对这个领域有点陌生,所以请容忍我。 我按照DSS本身提供的说明和演示进行操作,但没有效果。 我已经成功地使用PAdES B和PAdES T签署了Pdf,所以我的TSA服务设置正确。 我遇到的问题是,每次我尝试使用LTV签署Pdf时,都会出现以下错误:“eu.europ