以前的版本没有实现强制更新代码所以我在新版本(版本代码6)中添加了强制更新代码,并在Play商店上传了测试版本。现在为了测试,我创建了版本代码 5 的新发布版本并将其安装在我的设备中。在登录弹出窗口以更新和取消显示,但在单击更新按钮时,它正在导航到播放商店,其中有一个打开按钮而不是更新,尽管有更高版本可用。我搜索并尝试了 StackOverflow 上提到的不同方法,但没有一个对我有用。请帮我解决这个问题。
public class AppointActivity extends AppCompatActivity implements ForceUpdateChecker.OnUpdateNeededListener{
}
private void redirectStore(String updateUrl) {
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(updateUrl));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
@Override
public void onUpdateNeeded( final String updateUrl) {
AlertDialog dialog = new AlertDialog.Builder(this)//Alert dialog have implemetation have two options
.setTitle("New version available")
.setMessage("Please, update app to new version to continue searching.")
.setPositiveButton("Update Now",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
redirectStore(updateUrl);
}
}).setNegativeButton("Later",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create();
dialog.setCancelable(true);
dialog.show();
}
public class ForceUpdateChecker {
private static final String TAG = ForceUpdateChecker.class.getSimpleName();
public static final String KEY_UPDATE_REQUIRED = "force_update_required";
public static final String KEY_CURRENT_VERSION = "force_update_current_version";
public static final String KEY_UPDATE_URL = "force_update_store_url";
private OnUpdateNeededListener onUpdateNeededListener;
private Context context;
public interface OnUpdateNeededListener {
void onUpdateNeeded(String updateUrl);
}
public static Builder with(@NonNull Context context) {
return new Builder(context);
}
public ForceUpdateChecker(@NonNull Context context,
OnUpdateNeededListener onUpdateNeededListener) {
this.context = context;
this.onUpdateNeededListener = onUpdateNeededListener;
}
public void check() {
final FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance();
if (remoteConfig.getBoolean(KEY_UPDATE_REQUIRED)) {
String currentVersion = remoteConfig.getString(KEY_CURRENT_VERSION);
String appVersion = getAppVersion(context);
String updateUrl = remoteConfig.getString(KEY_UPDATE_URL);
if (!TextUtils.equals(currentVersion, appVersion)
&& onUpdateNeededListener != null) {
onUpdateNeededListener.onUpdateNeeded(updateUrl);
}
}
}
private String getAppVersion(Context context) {
String result = "";
try {
result = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0)
.versionName;
result = result.replaceAll("[a-zA-Z]|-", "");
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, e.getMessage());
errorClass.logError(e);
}
return result;
}
public static class Builder {
private Context context;
private OnUpdateNeededListener onUpdateNeededListener;
public Builder(Context context) {
this.context = context;
}
public Builder onUpdateNeeded(OnUpdateNeededListener onUpdateNeededListener) {
this.onUpdateNeededListener = onUpdateNeededListener;
return this;
}
public ForceUpdateChecker build() {
return new ForceUpdateChecker(context, onUpdateNeededListener);
}
public ForceUpdateChecker check() {
ForceUpdateChecker forceUpdateChecker = build();
forceUpdateChecker.check();
return forceUpdateChecker;
}
}
}
谢谢