检查应用程序是否在第一次运行

我是新来的android开发,我想安装一些应用程序的属性基于应用程序首次运行后安装。 有没有办法find应用程序第一次运行,然后设置其第一次运行属性?

以下是使用SharedPreferences实现“首次运行”检查的示例。

 public class MyActivity extends Activity { SharedPreferences prefs = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Perhaps set content view here prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE); } @Override protected void onResume() { super.onResume(); if (prefs.getBoolean("firstrun", true)) { // Do first run stuff here then set 'firstrun' as false // using the following line to edit/commit prefs prefs.edit().putBoolean("firstrun", false).commit(); } } } 

当代码运行prefs.getBoolean(...)如果在SharedPreferences使用“firstrun”键保存的boolean ,那么表示该应用程序从未运行过(因为没有任何东西用该键保存布尔值或用户已经清除了应用程序数据以强制执行“首次运行”场景)。 如果这不是第一次运行,那么行prefs.edit().putBoolean("firstrun", false).commit(); 将被执行,因此prefs.getBoolean("firstrun", true)实际上将返回false,因为它覆盖了作为第二个参数提供的默认true。

接受的答案不区分第一次运行和随后的升级。 只需在共享首选项中设置一个布尔值就会告诉你它是否是第一次安装应用程序后的第一次运行。 如果稍后想要升级应用程序并在升级的第一次运行时进行一些更改,则不能再使用该布尔值,因为共享首选项是跨升级保存的。

此方法使用共享首选项来保存版本代码而不是布尔值。

 private void checkFirstRun() { final String PREFS_NAME = "MyPrefsFile"; final String PREF_VERSION_CODE_KEY = "version_code"; final int DOESNT_EXIST = -1; // Get current version code int currentVersionCode = BuildConfig.VERSION_CODE; // Get saved version code SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); int savedVersionCode = prefs.getInt(PREF_VERSION_CODE_KEY, DOESNT_EXIST); // Check for first run or upgrade if (currentVersionCode == savedVersionCode) { // This is just a normal run return; } else if (savedVersionCode == DOESNT_EXIST) { // TODO This is a new install (or the user cleared the shared preferences) } else if (currentVersionCode > savedVersionCode) { // TODO This is an upgrade } // Update the shared preferences with the current version code prefs.edit().putInt(PREF_VERSION_CODE_KEY, currentVersionCode).apply(); } 

您可能会在您的主要活动中从onCreate调用此方法,以便每次启动应用程序时都会对其进行检查。

 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); checkFirstRun(); } private void checkFirstRun() { // ... } } 

如果需要,可以根据用户以前安装的版本来调整代码以执行特定的操作。

想法来自这个答案 。 这些也有帮助:

  • 你怎么能从应用程序的(布局)XMLvariables获得清单版本号?
  • 代码中的AndroidManifest.xml的用户版本名称值
  import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; import java.util.UUID; import android.content.Context; public class Util { // =========================================================== // // =========================================================== private static final String INSTALLATION = "INSTALLATION"; public synchronized static boolean isFirstLaunch(Context context) { String sID = null; boolean launchFlag = false; if (sID == null) { File installation = new File(context.getFilesDir(), INSTALLATION); try { if (!installation.exists()) { launchFlag = true; writeInstallationFile(installation); } sID = readInstallationFile(installation); } catch (Exception e) { throw new RuntimeException(e); } } return launchFlag; } private static String readInstallationFile(File installation) throws IOException { RandomAccessFile f = new RandomAccessFile(installation, "r");// read only mode byte[] bytes = new byte[(int) f.length()]; f.readFully(bytes); f.close(); return new String(bytes); } private static void writeInstallationFile(File installation) throws IOException { FileOutputStream out = new FileOutputStream(installation); String id = UUID.randomUUID().toString(); out.write(id.getBytes()); out.close(); } } > Usage (in class extending android.app.Activity) Util.isFirstLaunch(this); 

没有办法通过Android API知道。 您必须自己存储一些标志,并将其保存在SharedPreferenceEditor或使用数据库中。

如果你想在这个标志上添加许可证相关的东西,我build议你使用LVL库提供的模糊偏好编辑器。 这很简单,干净。

问候,斯蒂芬

只需检查一些偏好,默认值是第一次运行。 所以,如果你得到默认值,做你的初始化,并设置此偏好设置为不同的值,以表明该应用程序已经初始化。

没有可靠的方法来检测第一次运行,因为共享偏好方式并不总是安全的,用户可以从设置中删除共享偏好数据! 更好的方法是使用这里的答案有没有一个独特的Android设备ID? 获取设备的唯一ID并将其存储在您的服务器的某个位置,所以无论何时用户启动应用程序,您都可以请求服务器并检查数据库中是否存在该设备,或者是否是新的。

以下是使用SharedPreferences实现“forWhat”检查的示例。

  preferences = PreferenceManager.getDefaultSharedPreferences(context); preferencesEditor = preferences.edit(); public static boolean isFirstRun(String forWhat) { if (preferences.getBoolean(forWhat, true)) { preferencesEditor.putBoolean(forWhat, false).commit(); return true; } else { return false; } } 

这可能会帮助你

 public class FirstActivity extends Activity { SharedPreferences sharedPreferences = null; Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); sharedPreferences = getSharedPreferences("com.myAppName", MODE_PRIVATE); } @Override protected void onResume() { super.onResume(); if (sharedPreferences.getBoolean("firstRun", true)) { //You can perform anything over here. This will call only first time editor = sharedPreferences.edit(); editor.putBoolean("firstRun", false) editor.commit(); } } } 

我不确定这是检查它的好方法。 当用户使用设置中的“清除数据”button时,情况如何? SharedPreferences将被清除,然后再次捕获“首次运行”。 这是一个问题。 我想最好使用InstallReferrerReceiver

 SharedPreferences mPrefs; final String welcomeScreenShownPref = "welcomeScreenShown"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mPrefs = PreferenceManager.getDefaultSharedPreferences(this); // second argument is the default to use if the preference can't be found Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false); if (!welcomeScreenShown) { // here you can launch another activity if you like SharedPreferences.Editor editor = mPrefs.edit(); editor.putBoolean(welcomeScreenShownPref, true); editor.commit(); // Very important to save the preference } }