服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android实现关机后数据不会丢失问题

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

2022-11-04 13:54毛少雄 Android

这篇文章主要介绍了Android实现关机后数据不会丢失问题,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

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

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

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

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

数据类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?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

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

再重启

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

重启之后重新打开app

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

值还是没有变化测试成功

总结

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

原文链接:https://blog.csdn.net/weixin_43635647/article/details/102766005

延伸 · 阅读

精彩推荐
  • AndroidAndroid开发之电话拨号器和短信发送器实现方法

    Android开发之电话拨号器和短信发送器实现方法

    这篇文章主要介绍了Android开发之电话拨号器和短信发送器实现方法,结合实例形式较为详细的分析了Android电话拨号器和短信发送器的具体原理与实现步骤...

    傅荣康6932021-04-24
  • AndroidAndroid实现自定义dialog的代码

    Android实现自定义dialog的代码

    这篇文章主要介绍了Android实现自定义dialog的实例代码,代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下...

    给你留灯9422022-08-16
  • AndroidAndroid之RAS加密算法测试实例

    Android之RAS加密算法测试实例

    这篇文章介绍了Android之RAS加密算法测试实例,有需要的朋友可以参考一下...

    Android开发网8622021-02-05
  • AndroidAndroid获取SD卡中选中图片的路径(URL)示例

    Android获取SD卡中选中图片的路径(URL)示例

    一个图片上传功能需要提供上传图片在SD卡中的路径,总结了网上的一些列子,修改了一下,代码很简单,感兴趣的朋友可以参考下哈,希望对大家有所帮...

    Android开发网5882021-02-03
  • AndroidAndroid获取app应用程序大小的方法

    Android获取app应用程序大小的方法

    本文通过一段代码给大家介绍android获取app应用程序大小的方法,由于android对这种方法进行了封装,我们没有权限去调用这个方法,只能通过aidl,然后用...

    Android教程网8522021-04-12
  • AndroidAndroid集成腾讯X5实现文档浏览功能

    Android集成腾讯X5实现文档浏览功能

    Android内部没有控件来直接显示文档,跳转WPS或其他第三方文档App体验性不好,使用腾讯X5内核能很好的解决的这一问题这篇文章主要介绍了Android集成腾讯...

    hardWork_yulu6242022-11-03
  • Androidandroid实现添加耳机状态图标的方法

    android实现添加耳机状态图标的方法

    这篇文章主要介绍了android实现添加耳机状态图标的方法,较为详细的分析了Android实现添加耳机图标的原理与相关技巧,具有一定参考借鉴价值,需要的朋友可...

    pgalxx8252021-04-02
  • Androidandroid 开发教程之日历项目实践(一)

    android 开发教程之日历项目实践(一)

    决定开始学习 Android 平台下的软件开发,以日历作为实践项目,进行一周后,基本完成,有需要的朋友可以参考下...

    Android开发网11162020-12-29