Update / Uninstall上的SharedPreferences行为

时间:2011-08-18 06:48:57

标签: android sharedpreferences

我使用共享首选项来存储我的应用程序启动的次数。只有在第一次启动时,我才会显示一条欢迎消息,告诉用户该版本中的新功能和更改。

但是当我专注于重新安装应用程序或升级应用程序时,我无法删除以前的共享首选项。我想在重新安装软件或升级软件时获得对话框。

AppLauncher

public class AppLauncher {
    static long launch_count = 0;
    private static boolean isLaunch = false;

    public static void app_launched(Context mContext) {
        System.out.println("I m in AppLauncher");
        SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0);
        if (prefs.getBoolean("dontshowagain", false)) {
            return;
        }

        SharedPreferences.Editor editor = prefs.edit();

        // Increment launch counter

        launch_count = prefs.getLong("launch_count", 0);
        editor.putLong("launch_count", launch_count);

        System.out.println("launch_count=" + launch_count);
        if (launch_count == 0 || launch_count == 1) {
            // showLaunchDialog(mContext);
            isLaunch = true;
        }
        if (isLaunch == true) {
            showLaunchDialog(mContext);
            isLaunch = false;
        }
        editor.commit();
    }

    public static void showLaunchDialog(Context mcontext) {
        final Dialog dialog = new Dialog(mcontext);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.whatsnew);

        Button dismisButton = (Button) dialog.findViewById(R.id.dismisButtom);
        System.out.println("inside dialog_started");
        dismisButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
}

4 个答案:

答案 0 :(得分:15)

在更新的情况下,没有可用于删除共享首选项的挂钩。

Nikolay是对的,您可以保存应用的版本号。并将其与当前版本号进行比较。

要获取当前版本号码,请致电:

this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionCode

有关包信息中可用信息的详细信息,请参阅PackageInfoPackageManager上的文档。

答案 1 :(得分:11)

而不是保存boolean保存应用的版本号。如果当前应用程序的版本号更高(更新),请显示对话框并更新数字。

答案 2 :(得分:1)

如果你没有设置dontShowagin你默认会得到假。所以你想要显示对话框,下次不要。所以只需将优先级的值改为true,以便下次它可以工作。你也在增加计数器没有实际递增它。使用前一个+1。

SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0);
            if (prefs.getBoolean("dontshowagain", false)) {
                return;
            }

            SharedPreferences.Editor editor = prefs.edit();

            // Increment launch counter

            editor.putBoolean("dontShowagain",true);
            launch_count = prefs.getLong("launch_count", 0)+1;
            editor.putLong("launch_count", launch_count);

答案 3 :(得分:1)

试着听听这个意图:

ACTION_PACKAGE_ADDED
ACTION_PACKAGE_CHANGED
ACTION_PACKAGE_DATA_CLEARED
ACTION_PACKAGE_FIRST_LAUNCH
ACTION_PACKAGE_INSTALL  
ACTION_PACKAGE_REMOVED  
ACTION_PACKAGE_REPLACED 
ACTION_PACKAGE_RESTARTED

更多信息:http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_ADDED

相关问题