它可能对其他人有用。我试过这种方式
首先创建一个具有几个方法的类来启动 Play 商店并以这种方式获取应用程序版本代码和版本信息
public class CheckForUpdate {
public static final String ACTION_APP_VERSION_CHECK="app-version-check";
public static void launchPlayStoreApp(Context context)
{
final String appPackageName = context.getPackageName(); // getPackageName() from Context or Activity object
try {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
}
public static int getRemoteVersionNumber(Context context)
{
int versionCode=0;
try {
PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
String version = pInfo.versionName;
versionCode=pInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
}
其次创建另一个具有 sharedpreference 方法的 util 类以这种方式保存和检索版本代码
public class PreferenceUtils {
// this is for version code
private final String APP_VERSION_CODE = "APP_VERSION_CODE";
private SharedPreferences sharedPreferencesAppVersionCode;
private SharedPreferences.Editor editorAppVersionCode;
private static Context mContext;
public PreferenceUtils(Context context)
{
this.mContext=context;
// this is for app versioncode
sharedPreferencesAppVersionCode=mContext.getSharedPreferences(APP_VERSION_CODE,MODE_PRIVATE);
editorAppVersionCode=sharedPreferencesAppVersionCode.edit();
}
public void createAppVersionCode(int versionCode) {
editorAppVersionCode.putInt(APP_VERSION_CODE, versionCode);
editorAppVersionCode.apply();
}
public int getAppVersionCode()
{
return sharedPreferencesAppVersionCode.getInt(APP_VERSION_CODE,0); // as default version code is 0
}
}
最后,您可以在启动器活动或任何其他活动中使用您希望向用户显示警报对话框以更新应用程序(如果已更新)。
public class DashboardActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
...........
//check whether app is first time launched
AppLaunchChecker.onActivityCreate(this);
alertAppUpdate();
}
以这种方式实现 alertAppUpdate() 方法
private void alertAppUpdate()
{
int remoteVersionCode=CheckForUpdate.getRemoteVersionNumber(this);
PreferenceUtils preferenceUtils=new PreferenceUtils(this);
if(AppLaunchChecker.hasStartedFromLauncher(this))
{
preferenceUtils.createAppVersionCode(remoteVersionCode);
Log.i("First time","First time app is launched");
}
int existingVersionCode= preferenceUtils.getAppVersionCode();
if(remoteVersionCode>existingVersionCode)
{
/*
**
* app is updated, alert user to update app from playstore
* if app is updated then only save the version code in preferenceUtils
*
*/
AlertDialog.Builder dialogBuilder=AlertDialogBox.getAlertDialogBuilder(this,"Update available","Do you want to update your app now?");
dialogBuilder.setPositiveButton("Update Now", (dialogInterface, i) -> {
CheckForUpdate.launchPlayStoreApp(this);
Log.i("app update service","app is needed to update");
preferenceUtils.createAppVersionCode(remoteVersionCode);
});
dialogBuilder.setNegativeButton("Later",(dialogInterface,i)->{
});
dialogBuilder.show();
}
}
如果有任何错误,请告诉我。谢谢你。