Android 仅在应用程序升级时显示对话框
我想验证一个场景,其中应用程序中的已升级到最新版本,然后执行一个操作。在用户的手机上,应用程序的版本是2.4。他/她从Play Store升级到最新版本(2.5)。我将检查它是否是升级后的第一次运行,并显示一个对话框。(我使用这里给出的解决方案:验证首次发射) 但是,在将更新推送到Play Store之前,如何验证这种情况。我试过的是:Android 仅在应用程序升级时显示对话框,android,Android,我想验证一个场景,其中应用程序中的已升级到最新版本,然后执行一个操作。在用户的手机上,应用程序的版本是2.4。他/她从Play Store升级到最新版本(2.5)。我将检查它是否是升级后的第一次运行,并显示一个对话框。(我使用这里给出的解决方案:验证首次发射) 但是,在将更新推送到Play Store之前,如何验证这种情况。我试过的是: 从应用商店手动安装应用程序 从Android Studio以调试/发布模式运行最新版本 但是,当我执行上述操作时,会出现以下错误: 我还尝试从我拥有的旧apk
是否有其他方法可以验证该对话框是否仅在用户将应用升级到最新版本时出现?我认为您会遇到此错误,因为应用商店版本(由发布密钥签名)和studio运行版本(由调试密钥签名)之间的签名不相同(存在于android数据文件夹中) 因此,这是关于错误的,您必须构建APK,就好像您要将其上载到商店一样,并使用您的发布密钥库对其进行签名…然后将其安装到设备上(使用
adb install
或adb push,然后从移动设备安装,或者将APK复制到移动内存,然后从移动设备安装…等等),可能吗
现在关于第二部分,检测应用程序的第一次运行/应用程序更新后检测第一次运行,您可以按照该答案的建议使用SharedReferences
。但这还不够,因为它不受应用程序版本的链接/影响,而这正是升级后检测第一次运行所需要的
您需要做的是获取在清单文件android:versionCode
中指定的应用程序版本代码(即整数),并且每次将版本上载到PlayStore时都必须增加该代码
此代码将执行您需要的操作(您可以在希望显示对话框的活动/片段中使用它)
prefs.getInt(“last\u version\u code”,-1)
这将获取键last\u version\u code
首选项中最后保存的值,如果没有值(第一次运行),它将返回默认值-1
if
检查从首选项>0返回的值(不是第一次运行)
它与清单中的不相等。这意味着(升级)
在==-1
的情况下,您只需编写版本代码,以便下次尝试获取该值时,它将返回一个数字(保存的最新版本代码)
如果要将其用作方法(可在多个位置调用)
如果应用程序已更新,请创建一个返回true/false的方法
public static boolean appWasUpdated(Context context){
//this code gets current version-code (after upgrade it will show new versionCode)
PackageManager manager = context.getPackageManager();
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
int versionCode = info.versionCode;
SharedPreferences prefs = context.getSharedPreferences("yourAppName", Context.MODE_PRIVATE);
if(prefs.getInt("last_version_code", -1) > 0){
if(prefs.getInt("last_version_code", -1) != versionCode){
//save current versionCode: 1st-run after upgrade
prefs.edit().putInt("last_version_code", versionCode).commit();
return true;
} //no need for else, because app version did not change...
}else{
//save current versionCode for 1st-run ever
prefs.edit().putInt("last_version_code", versionCode).commit();
}
return false;
}
只需从activity/fragment调用此方法
if(appWasUpdated(this)){
showMyDialog();
}
我认为您会遇到此错误,因为store版本(由发布密钥签名)和studio运行版本(由调试密钥签名)(存在于android数据文件夹中)之间的签名不同 因此,这是关于错误的,您必须构建APK,就好像您要将其上载到商店一样,并使用您的发布密钥库对其进行签名…然后将其安装到设备上(使用
adb install
或adb push,然后从移动设备安装,或者将APK复制到移动内存,然后从移动设备安装…等等),可能吗
现在关于第二部分,检测应用程序的第一次运行/应用程序更新后检测第一次运行,您可以按照该答案的建议使用SharedReferences
。但这还不够,因为它不受应用程序版本的链接/影响,而这正是升级后检测第一次运行所需要的
您需要做的是获取在清单文件android:versionCode
中指定的应用程序版本代码(即整数),并且每次将版本上载到PlayStore时都必须增加该代码
此代码将执行您需要的操作(您可以在希望显示对话框的活动/片段中使用它)
prefs.getInt(“last\u version\u code”,-1)
这将获取键last\u version\u code
首选项中最后保存的值,如果没有值(第一次运行),它将返回默认值-1
if
检查从首选项>0返回的值(不是第一次运行)
它与清单中的不相等。这意味着(升级)
在==-1
的情况下,您只需编写版本代码,以便下次尝试获取该值时,它将返回一个数字(保存的最新版本代码)
如果要将其用作方法(可在多个位置调用)
如果应用程序已更新,请创建一个返回true/false的方法
public static boolean appWasUpdated(Context context){
//this code gets current version-code (after upgrade it will show new versionCode)
PackageManager manager = context.getPackageManager();
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
int versionCode = info.versionCode;
SharedPreferences prefs = context.getSharedPreferences("yourAppName", Context.MODE_PRIVATE);
if(prefs.getInt("last_version_code", -1) > 0){
if(prefs.getInt("last_version_code", -1) != versionCode){
//save current versionCode: 1st-run after upgrade
prefs.edit().putInt("last_version_code", versionCode).commit();
return true;
} //no need for else, because app version did not change...
}else{
//save current versionCode for 1st-run ever
prefs.edit().putInt("last_version_code", versionCode).commit();
}
return false;
}
只需从activity/fragment调用此方法
if(appWasUpdated(this)){
showMyDialog();
}
adb Push myapp.APK/sdcard/0/
adb install
在这种情况下不起作用adb Push myapp.APK/sdcard/0/
adb install
在这种情况下不起作用