检测Android应用程序的升级和设置应用程序类布尔的最终用户许可协议显示/隐藏 [英] Detect Android app upgrade and set Application class boolean for show/hide of EULA

查看:145
本文介绍了检测Android应用程序的升级和设置应用程序类布尔的最终用户许可协议显示/隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发现当我的应用程序已使用一个BroadcastReceiver升级和设置在我的应用类一个布尔值。该布尔将配合使用其他一些布尔,以确定是否要显示的EULA对话框给用户。

我相信我已经正确地得到了这一切的设置,但最终用户许可协议仍然显示当它不应该了。特别是当用户已经接受了最终用户许可协议在previous版本,该协议并没有在版本改变升级到(手动由我设定),以及应用程序正在升级。

我相信这是不工作的原因是因为我的应用程序没有运行,因此isAppUpgrade()方法不会被调用,并设置正确的布尔标志。有人可以证实这一点的话,或者是有什么错我的code?

仅供参考 - 该EULA.show(活动,布尔,布尔)静态方法被调用我的主要活动第一件事

下面是一些code

应用类

 公共类MFCApplication扩展应用{

    私人布尔isUpgrade = FALSE;

    / **
     *返回在这个版本的应用程序的最终用户许可协议是否已经改变了手动设定值
     * @返回真/假
     * /
    公共布尔hasEULAChanged(){
        返回false;
    }

    / **
     *返回应用程序是否已经被升级。设置由UpgradeBroadcastReceiver
     * @返回真/假
     * /
    公共布尔isAppUpgrade(){
        返回isUpgrade;
    }

    / **
     *方法调用UpgradeBroadcastReceiver如果应用程序已经升级
     * /
    公共无效setAppIsUpgrade(){
        this.isUpgrade = TRUE;
    }
}
 

的BroadcastReceiver

 公共类UpgradeBroadcastReceiver扩展的BroadcastReceiver {

    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        如果(意向== NULL)
            返回;
        如果(背景== NULL)
            返回;

        串动= intent.getAction();
        如果(动作== NULL)
            返回;

        如果(action.equals(Intent.ACTION_PACKAGE_REPLACED)){
            MFCApplication对myApp =((MFCApplication)((活动)上下文).getApplication());

            myApp.setAppIsUpgrade();
        }
    }
}
 

EULA类

 公共类EULA {

    私有静态最后弦乐EULA_ASSET =最终用户许可协议;
    私有静态最后弦乐EULA_ preFERENCES =最终用户许可协议;
    私有静态活动mActivity;

    私有静态PackageInfo getPackageInfo(){
        PackageInfo PI = NULL;
        尝试 {
            PI = mActivity.getPackageManager()getPackageInfo(mActivity.getPackageName(),PackageManager.GET_ACTIVITIES)。
        }赶上(PackageManager.NameNotFoundException前){
            ex.printStackTrace();
        }
        返回PI;
    }

    公共静态布尔秀(活动活动,布尔hasEULAChanged,布尔isAppUpgrade){
        mActivity =活动;
        最后一个共享preferences preferences = activity.getShared preferences(EULA_ preFERENCES,Activity.MODE_PRIVATE);
        最终PackageInfo packageInfo = getPackageInfo();
        字符串EULA preF = preferences.getString(EULA_ preFERENCES,0);
        布尔eulaVersionAccepted = packageInfo.versionName.equals(EULA preF);
        如果(eulaVersionAccepted&安培;!及(hasEULAChanged || isAppUpgrade)!){
            // EULA中应这里示出,但它不是
            返回false;
        }
        返回true;
    }
}
 

应用程序清单

 <接收机器人:名称=。helpers.UpgradeBroadcastReceiver>
    <意向滤光器>
        <作用机器人:名称=android.intent.action.PACKAGE_REPLACED/>
        <数据机器人:计划=包机器人:路径=com.hookedroid.fishingcompanion/>
    &所述; /意图滤光器>
< /接收器>
 

解决方案

这是很容易只检查当前的应用程序版本。

  PackageInfo packageInfo = activity.getPackageManager()
    .getPackageInfo(activity.getPackageName(),0);
INT版本code = packageInfo.version code;
 

当你的应用程序启动,你检查你的共享preferences与版本code的整数值。如果没有,或者如果它不匹配,显示EULA。用户接受EULA后,写的版本code值的共享preferences。

版本code 将匹配您的清单存储的版本号。

I'm attempting to detect when my application has been upgraded using a BroadcastReceiver and set a boolean in my Application Class. This boolean will be used in conjunction with a few other booleans to determine whether or not to show the EULA dialog box to the user.

I belive I've got it all setup correctly, but the EULA is still showing up when it shouldn't. Specifically when the user has already accepted the EULA in a previous version, the EULA hasn't changed in the version being upgraded to(Manually set by me), and the app is being upgraded.

I believe the reason this isn't working is because my Application isn't running and therefore the isAppUpgrade() method isn't being called and setting the correct boolean flag. Can somebody confirm this is the case, or is there something wrong in my code?

FYI - The EULA.show(Activity, boolean, boolean) static method is being called first thing in my Main activity.

Here's some code

Application Class

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 Class

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

Application Manifest

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

解决方案

It's much easier to just check your current app version.

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

When your app starts, you check your SharedPreferences for an integer value with the version code. If there is none, or if it doesn't match, display the EULA. After the user accepts the EULA, write the versionCode value to the SharedPreferences.

versionCode will match the version number you store in the Manifest.

这篇关于检测Android应用程序的升级和设置应用程序类布尔的最终用户许可协议显示/隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆