0

以前的版本没有实现强制更新代码所以我在新版本(版本代码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;
        }
    }

}

谢谢

4

3 回答 3

1

如果您通过 adb 安装应用程序或从其他来源安装应用程序,Google play 将显示“打开”而不是“更新”。

如果您的应用程序是通过 Google Play 安装的,或者应该通过系统应用程序安装以在 Google Play 中显示更新,

于 2020-01-30T06:07:58.090 回答
0

这是 Play 商店的问题。我也面临同样的问题。如果您返回并再次访问同一个应用程序,它将显示更新按钮而不是打开。

题外话:为什么你不使用 InAppUpdate 而不是你的代码?对于 InAppUpdate,请选择以下我的答案链接

https://stackoverflow.com/a/59930031/11158194

我希望这可以帮助你!

谢谢你。

于 2020-01-28T06:50:27.247 回答
-1

在应用级别的 build.gradle 文件中检查您的版本名称和版本代码您可以查看此链接“ https://github.com/hummatli/AndroidAppUpdater

于 2020-01-28T06:48:41.410 回答