在Android设备上安装应用程序后,仅运行一次代码 [英] Run code only once after an application is installed on Android device

查看:52
本文介绍了在Android设备上安装应用程序后,仅运行一次代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在应用程序安装后运行一段代码.执行完该代码后,即使升级,也不应再次调用该特定代码.

I want to run a piece of code once only after the application is installed. After it has been executed, that particular piece of code should not be called again, even for an upgrade.

推荐答案

  1. 检查布尔X是否在共享首选项中为True
  2. 如果不是:
    一种.运行特殊代码
    b.在共享首选项中将x保存为true
  1. Check if boolean X is True in shared preferences
  2. If not:
    a. Run the special code
    b. Save x as true in shared preferences

例如:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if(!prefs.getBoolean("firstTime", false)) {
    // run your one time code
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("firstTime", true);
    editor.commit();
}

这篇关于在Android设备上安装应用程序后,仅运行一次代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆