共享preferences上更新/卸载行为行为、preferences

2023-09-05 02:25:28 作者:暮雨咋歇

我使用的共享preferences存储的时候我的应用程序已经启动的次数。只有在第一次启动我显示欢迎信息,告诉有关新功能和变化,该版本的用户。

但正如我吸附在重新安装应用程序或升级应用程序我不能够删除previous共享preferences。我想获得的对话框,当我重新安装软件或升级它。

AppLauncher

 公共类AppLauncher {
    静态长launch_count = 0;
    私有静态布尔isLaunch = FALSE;

    公共静态无效app_launched(上下文mContext){
        的System.out.println(我M在AppLauncher);
        共享preferences preFS = mContext.getShared preferences(apprater,0);
        如果(prefs.getBoolean(dontshowagain,FALSE)){
            返回;
        }

        共享preferences.Editor编辑器= prefs.edit();

        //增加启动柜

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

        的System.out.println(launch_count =+ launch_count);
        如果(launch_count == 0 || launch_count == 1){
            // showLaunchDialog(mContext);
            isLaunch = TRUE;
        }
        如果(isLaunch ==真){
            showLaunchDialog(mContext);
            isLaunch = FALSE;
        }
        editor.commit();
    }

    公共静态无效showLaunchDialog(上下文mcontext){
        最后一个对话框对话框=新的对话框(mcontext);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.whatsnew);

        按钮dismisButton =(按钮)dialog.findViewById(R.id.dismisButtom);
        的System.out.println(里dialog_started);
        dismisButton.setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(查看为arg0){
                dialog.dismiss();
            }
        });
        dialog.show();
    }
}
 

解决方案

没有钩子,你可以用它来删除共享preferences在更新的情况下。

尼古拉是正确的,你可以保存你的应用程序的版本号。并与当前版本号进行比较。

要获得当前版本号电话:

  this.getPackageManager()。getPackageInfo(this.getPackageName(),0).version code
 
怎么卸载电脑上的百图秀 分享卸载的方法

有关更多信息哪些信息是在包中的信息可以读取的PackageInfo而PackageManager.

I am using shared preferences to store the number of times my application has been launched. Only on the first launch i display an Welcome message telling the user about the new features and changes in that version.

But as i absorbed on reinstalling the app or upgrading the application i am not able to remove the previous shared preferences. i would like to get the dialog when i reinstall the software or upgrade it too.

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();
    }
}

解决方案

There is no hook that you can use to erase the shared preferences in the case of an update.

Nikolay is right you can save the version number of your app. And compare it with the current version number.

To obtain the current version number call:

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

For more info on what information is available in the package info read the Documentation on the PackageInfo and the PackageManager.