检测 Android 应用升级并设置应用程序类布尔值以显示/隐藏 EULA

2022-09-01 02:51:29

我正在尝试检测我的应用程序何时已使用 BroadcastReceiver 升级,并在我的应用程序类中设置布尔值。此布尔值将与其他几个布尔值结合使用,以确定是否向用户显示 EULA 对话框。

我相信我已经正确设置了所有设置,但是EULA仍然在不应该出现的时候出现。具体而言,当用户已在以前的版本中接受 EULA 时,正在升级到的版本(由我手动设置)中的 EULA 未发生更改,并且正在升级应用。

我相信这不起作用的原因是因为我的应用程序没有运行,因此没有调用isAppUpgrade()方法并设置正确的布尔标志。有人可以确认这是事实,还是我的代码中有问题?

仅供参考 - EULA.show(活动,布尔,布尔)静态方法在我的主要活动中被调用。

下面是一些代码

应用程序类

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;
    }
}

广播接收者

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();
        }
    }
}

最终用户许可协议类

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;
    }
}

应用程序清单

<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>

答案 1

只需检查您当前的应用程序版本就容易得多。

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

当 App 启动时,您可以检查“共享首选项”中是否有带有版本号的整数值。如果没有,或者不匹配,请显示 EULA。用户接受 EULA 后,将版本代码值写入共享首选项。

versionCode将与您存储在清单中的版本号匹配。


答案 2

根据原始方法,我已经管理它来检测是否使用BroadcastReceiver升级了应用程序。

public class YourUpgradeReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        Uri packageName = intent.getData();
        if(packageName.toString().equals("package:" + context.getPackageName())){
            //Application was upgraded
        }
    } 
}

在你的清单中

 <receiver android:name=".YourUpgradeReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_REPLACED"/>
                <data android:scheme="package"/>
            </intent-filter>
 </receiver>

推荐