gpt4 book ai didi

android - 更新/卸载时的 SharedPreferences 行为

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:48:27 25 4
gpt4 key购买 nike

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

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

应用启动器

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

最佳答案

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

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

获取当前版本号调用:

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

有关包信息中可用信息的更多信息,请阅读 PackageInfo 上的文档和 PackageManager .

关于android - 更新/卸载时的 SharedPreferences 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7103453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com