我试图检测何时使用BroadcastReceiver升级了我的应用程序,并在我的应用程序类中设置了一个 boolean 值。该 boolean 值将与其他几个 boolean 值一起使用,以确定是否向用户显示EULA对话框。

我相信我已经正确设置了所有设置,但是EULA仍然会在不应该设置的情况下显示出来。具体来说,当用户已经接受了先前版本中的EULA时,EULA不会在要升级到的版本中更改(由我手动设置),并且正在升级应用程序。

我认为这不起作用的原因是因为我的应用程序未运行,因此未调用isAppUpgrade()方法并设置了正确的 boolean 标志。有人可以确认是这种情况,还是我的代码有问题?

仅供引用-EULA.show(Activity,boolean,boolean)静态方法在我的Main Activity 中被称为第一件事。

这是一些代码

应用程序类

public class MFCApplication extends Application {

    private boolean isUpgrade = false;

    /**
     * Returns a manually set value of whether the EULA has changed in this version of the App
     * @return true/false
     */
    public boolean hasEULAChanged() {
        return false;
    }

    /**
     * Returns whether or not the application has been upgraded.  Set by the UpgradeBroadcastReceiver
     * @return true/false
     */
    public boolean isAppUpgrade() {
        return isUpgrade;
    }

    /**
     * Method called by UpgradeBroadcastReceiver if the App has been upgraded
     */
    public void setAppIsUpgrade() {
        this.isUpgrade = true;
    }
}

BroadcastReceiver
public class UpgradeBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null)
            return;
        if (context == null)
            return;

        String action = intent.getAction();
        if (action == null)
            return;

        if (action.equals(Intent.ACTION_PACKAGE_REPLACED)) {
            MFCApplication myApp = ((MFCApplication)((Activity)context).getApplication());

            myApp.setAppIsUpgrade();
        }
    }
}

EULA类
public class EULA {

    private static final String EULA_ASSET = "EULA";
    private static final String EULA_PREFERENCES = "eula";
    private static Activity mActivity;

    private static PackageInfo getPackageInfo() {
        PackageInfo pi = null;
        try {
            pi = mActivity.getPackageManager().getPackageInfo(mActivity.getPackageName(), PackageManager.GET_ACTIVITIES);
        } catch (PackageManager.NameNotFoundException ex) {
            ex.printStackTrace();
        }
        return pi;
    }

    public static boolean show(Activity activity, boolean hasEULAChanged, boolean isAppUpgrade) {
        mActivity = activity;
        final SharedPreferences preferences = activity.getSharedPreferences(EULA_PREFERENCES, Activity.MODE_PRIVATE);
        final PackageInfo packageInfo = getPackageInfo();
        String eulaPref = preferences.getString(EULA_PREFERENCES, "0");
        boolean eulaVersionAccepted = packageInfo.versionName.equals(eulaPref);
        if (!eulaVersionAccepted && (hasEULAChanged || !isAppUpgrade)) {
            //The EULA should be shown here, but it isn't
            return false;
        }
        return true;
    }
}

申请 list
<receiver android:name=".helpers.UpgradeBroadcastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.PACKAGE_REPLACED" />
        <data android:scheme="package" android:path="com.hookedroid.fishingcompanion" />
    </intent-filter>
</receiver>

最佳答案

仅检查您当前的应用程序版本会容易得多。

PackageInfo packageInfo = activity.getPackageManager()
    .getPackageInfo(activity.getPackageName(), 0);
int versionCode = packageInfo.versionCode;

应用启动时,您将使用版本代码检查SharedPreferences中的整数值。如果没有,或者不匹配,则显示EULA。用户接受EULA之后,将versionCode值写入SharedPreferences。
versionCode将与您在 list 中存储的版本号匹配。

10-06 07:16