zoukankan      html  css  js  c++  java
  • 2月4日

    今天主要研究了安卓系统关于如何保持后台运行的相关方面。

    package com.eb.myapplicationsd;

    import androidx.appcompat.app.AppCompatActivity;
    import androidx.databinding.DataBindingUtil;
    import androidx.lifecycle.SavedStateViewModelFactory;
    import androidx.lifecycle.ViewModelProvider;
    import androidx.lifecycle.ViewModelProviders;
    import android.os.Bundle;
    import com.eb.myapplicationsd.databinding.ActivityMainBinding;
     
    public class MainActivity extends AppCompatActivity {
            MyViewModel myViewModel;
            ActivityMainBinding binding;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            binding=DataBindingUtil.setContentView(this,R.layout.activity_main);
             myViewModel = ViewModelProviders.of(thisnew SavedStateViewModelFactory(getApplication(),this)).get(MyViewModel.class);
             binding.setData(myViewModel);
             binding.setLifecycleOwner(this);}
     
        @Override
        protected void onPause() {
            super.onPause();
            myViewModel.save();}}
     
     
    package com.eb.myapplicationsd;
     
    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.SavedStateHandle;
    import androidx.lifecycle.ViewModel;
     
    public class MyViewModel extends AndroidViewModel {
        SavedStateHandle savedStateHandle;
        String key=getApplication().getResources().getString(R.string.data_key);
        String shpname=getApplication().getResources().getString(R.string.shp_name);
        public MyViewModel(@NonNull Application application,SavedStateHandle savedStateHandle) {
            super(application);
            this.savedStateHandle=savedStateHandle;
            if(!savedStateHandle.contains(key))
            {}}
      public  LiveData<Integer>getNumber(){
            return savedStateHandle.getLiveData(key);}
        public void load(){
            SharedPreferences sharedPreferences=getApplication().getSharedPreferences(shpname, Context.MODE_PRIVATE);
            int x=sharedPreferences.getInt(key,0);
            savedStateHandle.set(key,x);}
        public void save(){
            SharedPreferences sharedPreferences=getApplication().getSharedPreferences(shpname,Context.MODE_PRIVATE);
            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.putInt(key,getNumber().getValue());
            editor.apply();}
        public void add(int x){
            savedStateHandle.set(key,getNumber().getValue()+x);}}
  • 相关阅读:
    C#开发中is和as的区别
    Winform开发框架之系统登录实现
    C#几个经常犯错误汇总
    JavaScript事件冒泡简介及应用
    在C#的winForm程序中调用和执行javascript
    C#关于托管程序和非托管程序的区别
    分布式计算 网格计算 并行计算 云计算
    (转)960的秘密
    集群概念:集群技术简介(转)
    好用的Sql格式化工具
  • 原文地址:https://www.cnblogs.com/dg1137/p/12268036.html
Copyright © 2011-2022 走看看