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

hoo*_*d82 35 java android broadcastreceiver

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

我相信我已经完全正确地设置了它,但EULA仍然会在不应该出现的时候出现.特别是当用户已经在先前版本中接受了EULA时,EULA在升级版本(由我手动设置)中没有更改,并且应用程序正在升级.

我认为这不起作用的原因是因为我的应用程序没有运行,因此没有调用isAppUpgrade()方法并设置正确的布尔标志.有人可以确认是这种情况,或者我的代码中有什么问题吗?

仅供参考 - 在我的Main活动中首先调用EULA.show(Activity,boolean,boolean)静态方法.

这是一些代码

应用类

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

广播接收器

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

应用程序清单

<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>
Run Code Online (Sandbox Code Playgroud)

Ebo*_*ike 63

检查您当前的应用版本要容易得多.

PackageInfo packageInfo = activity.getPackageManager()
    .getPackageInfo(activity.getPackageName(), 0);
int versionCode = packageInfo.versionCode;
Run Code Online (Sandbox Code Playgroud)

当您的应用启动时,您可以使用版本代码检查SharedPreferences的整数值.如果没有,或者不匹配,则显示EULA.用户接受EULA后,将versionCode值写入SharedPreferences.

versionCode 将匹配您在Manifest中存储的版本号.

  • 您还可以使用BuildConfig.VERSION_CODE来获取在应用程序的build.gradle中设置的版本代码。 (3认同)

Mar*_*ies 25

根据最初的方法,我设法检测是否使用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
        }
    } 
}
Run Code Online (Sandbox Code Playgroud)

并在你的清单

 <receiver android:name=".YourUpgradeReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_REPLACED"/>
                <data android:scheme="package"/>
            </intent-filter>
 </receiver>
Run Code Online (Sandbox Code Playgroud)

  • 你必须为自己提出一个简单的问题:你需要一个基于事件的方法,还是足以在你的应用程序的某个点检查它?如果您希望在更新应用程序时获得该信息,请转到我的解决方案(例如:后台服务).如果在下一个应用程序启动时知道它就足够了,请采取其他解决方案.但你也可以采取我的解决方案并写一些东西到prefs,你可以事后检查,以具有相同的行为,如理论上的EboMikes方法 (2认同)

sha*_*amd 9

检测应用程序是否已更新

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
int versionCode = sharedPreferences.getInt("VERSION_CODE", BuildConfig.VERSION_CODE);

if(versionCode != BuildConfig.VERSION_CODE) {
    onAppUpdated();
}

sharedPreferences.edit().putInt("VERSION_CODE", BuildConfig.VERSION_CODE).apply();
Run Code Online (Sandbox Code Playgroud)