如何使用Android實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)不會(huì)丟失問題

這篇文章將為大家詳細(xì)講解有關(guān)如何使用Android實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)不會(huì)丟失問題,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

成都創(chuàng)新互聯(lián)網(wǎng)絡(luò)公司擁有十余年的成都網(wǎng)站開發(fā)建設(shè)經(jīng)驗(yàn),上千多家客戶的共同信賴。提供成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)站開發(fā)、網(wǎng)站定制、買友情鏈接、建網(wǎng)站、網(wǎng)站搭建、成都響應(yīng)式網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)師打造企業(yè)風(fēng)格,提供周到的售前咨詢和貼心的售后服務(wù)

要實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)也不會(huì)丟失,需要使用到 AndroidViewModel,SaveStateHandle 和 SharePreferences 要達(dá)到的目的就是將數(shù)據(jù)保存成這個(gè)亞子

就不會(huì)出現(xiàn)app在異常閃退或者關(guān)機(jī)后數(shù)據(jù)的丟失了注意在使用SaveStateHandle和binding的時(shí)候需要在gradle里面設(shè)置一波

數(shù)據(jù)類

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); }}//這段代碼里面有幾個(gè)重要的點(diǎn)就是在使用handle的時(shí)候要注意使用的數(shù)據(jù)是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(); }}//這段代碼的重點(diǎn)就是使用onPause這個(gè)聲明周期的函數(shù)來調(diào)用save()函數(shù)

布局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>

關(guān)于“如何使用Android實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)不會(huì)丟失問題”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

網(wǎng)頁題目:如何使用Android實(shí)現(xiàn)關(guān)機(jī)后數(shù)據(jù)不會(huì)丟失問題
文章轉(zhuǎn)載:http://muchs.cn/article32/jpidpc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、電子商務(wù)ChatGPT、網(wǎng)站設(shè)計(jì)Google、手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)