> 19. AndroidViewModel_第1张图片" width="650" height="629" style="border:1px solid black;">

public class MyViewModel extends AndroidViewModel {    private SavedStateHandle handle;    /* 获取应用资源,以提供此类访问权限 */    private String key = getApplication().getResources().getString(R.string.Key);    private String shp_name = getApplication().getResources().getString(R.string.shp_name);    public MyViewModel(@NonNull Application application, SavedStateHandle handle) {        super(application);        this.handle = handle;        if (!handle.contains(key)) {            load();        }    }    private LiveData<Integer> getNumber() {        return handle.getLiveData(key);    }    private void load() {        SharedPreferences shp = getApplication().getSharedPreferences(shp_name, Context.MODE_PRIVATE);        int x = shp.getInt(key, 0);        handle.set(key, x);    }    void save() {        SharedPreferences shp = getApplication().getSharedPreferences(shp_name, Context.MODE_PRIVATE);        SharedPreferences.Editor editor = shp.edit();        editor.putInt(key, (getNumber().getValue() == null ? 0 : getNumber().getValue()));        editor.apply();    }    void add(int x) {        handle.set(key, (getNumber().getValue() == null ? 0 : getNumber().getValue()) + x);    }}

更多相关文章

  1. Read Contacts information in Android(安卓)system.
  2. Android实用代码片段(一)
  3. Android(安卓)获取本机WIFI及3G网络IP
  4. android将后台应用置顶及moveTaskToFront的用法
  5. Android:获取屏幕完整宽高,包含状态栏
  6. android设置gps自动开启
  7. android > 获取图片 从 本地 /相机
  8. [CSDN]Android应用程序启动过程源代码分析
  9. Android(安卓)adb shell后面可用的常用命令详细列举

随机推荐

  1. RelativeLayout
  2. Android(安卓)studio 遇到Android(安卓)S
  3. inputtype
  4. Android(安卓)单选按钮Radio的使用
  5. Android中Dialog对话框
  6. scrollView嵌套recyclerView 显示不全
  7. Android总结篇系列:Android(安卓)权限
  8. Android(安卓)WebView
  9. EditText设置键盘操作
  10. android MotionEvent中getX()和getRawX()