简体   繁体   English

如何跟踪应用程序是否首次在Android中启动?

[英]How to track if an app is launching for the first time in Android?

I am developing an app in which for only the first time it is launched, I want to perform some actions. 我正在开发一个应用程序,该应用程序一次启动时要执行一些操作。 Now I considered using Shared Preferences till I hit the dilemma that I'd have to initialize that on Oncreate itself, and every time I launch the app, the shared preferences would be overwritten. 现在,我考虑使用共享首选项,直到遇到必须在Oncreate本身上对其进行初始化的难题,并且每次启动该应用程序时,共享首选项都会被覆盖。

So, I am considering checking whether a particular type variable itself exists on the Shared Preferences or not, but I am stuck there too. 因此,我正在考虑检查共享首选项上是否存在特定类型变量本身,但我也被卡在那儿。 Now is there a simpler way that I am overlooking? 现在有没有一种我可以忽略的简单方法? Any help would be greatly appreciated. 任何帮助将不胜感激。

Use this first time SharePrefrences Code: 首次使用此SharePrefrences代码:

SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
        if (!prefs.getBoolean("Time", false)) {

                           // run your one time code

            SharedPreferences.Editor editor = prefs.edit();
            editor.putBoolean("Time", true);
            editor.commit();
        }

This sharedpreference is run only one time when first time is application is launched. 第一次启动应用程序时,此共享首选项仅运行一次。 This is work for me. 这对我来说是工作。

For this you need to check like this.. 为此,您需要像这样检查。

/**
 * checks for the whether the Application is opened first time or not
 * 
 * @return true if the the Application is opened first time
 */
public boolean isFirstTime() {
    File file = getDatabasePath("your file");
    if (file.exists()) {
        return false;
    }
    return true;
}

if the file is exist its not the first time other wise its the first time.. 如果文件存在,则不是第一次,否则是第一次。

SharePreferences is a good choice. SharePreferences是一个不错的选择。

public class ShortCutDemoActivity extends Activity {

// Create Preference to check if application is going to be called first
// time.
SharedPreferences appPref;
boolean isFirstTime = true;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Get preference value to know that is it first time application is
    // being called.
    appPref = getSharedPreferences("isFirstTime", 0);
    isFirstTime = appPref.getBoolean("isFirstTime", true);

    if (isFirstTime) {
        // Create explicit intent which will be used to call Our application
        // when some one clicked on short cut
        Intent shortcutIntent = new Intent(getApplicationContext(),
                ShortCutDemoActivity.class);
        shortcutIntent.setAction(Intent.ACTION_MAIN);
        Intent intent = new Intent();

        // Create Implicit intent and assign Shortcut Application Name, Icon
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Demo");
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                Intent.ShortcutIconResource.fromContext(
                        getApplicationContext(), R.drawable.logo));
        intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        getApplicationContext().sendBroadcast(intent);

        // Set preference to inform that we have created shortcut on
        // Homescreen
        SharedPreferences.Editor editor = appPref.edit();
        editor.putBoolean("isFirstTime", false);
        editor.commit();

    }
}

} }

And modify your Androidmanifest.xml 并修改您的Androidmanifest.xml

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM