前言

Android高版本上安装一些旧的APP,会存在会弹出对话框,内容:“此应用专为旧版Android打造,因此可能无法正常运行。请尝试检查更新或与开发者联系”。

这样不是很友好,客户要求去掉。

正文

原因

本质是Android高版本对启动应用的支持SDK的检测。

下面是网上分析的,摘抄于此。

应用启动,startActivity时,流程会执行到realStartActivityLocked方法。

final boolean realStartActivityLocked(ActivityRecord r, ProcessRecord app, boolean andResume, boolean checkConfig) throws RemoteException {
    try {
        // ...
        mService.getAppWarningsLocked().onStartActivity(r);
        // ...
    } catch (RemoteException e) {
        // ...
    }
}

onStartActivity方法实现:

public void onStartActivity(ActivityRecord r) {
    showUnsupportedCompileSdkDialogIfNeeded(r);
    showUnsupportedDisplaySizeDialogIfNeeded(r);
    showDeprecatedTargetDialogIfNeeded(r);
}

然后真正判断的就是showDeprecatedTargetDialogIfNeeded()

public void showDeprecatedTargetDialogIfNeeded(ActivityRecord r) {
    if (r.appInfo.targetSdkVersion < Build.VERSION.MIN_SUPPORTED_TARGET_SDK_INT) {
        mUiHandler.showDeprecatedTargetDialog(r);
    }
}

这里有对启动App的skd版本判断。

在Android P(9)上,这个Build.VERSION.MIN_SUPPORTED_TARGET_SDK_INT值就是17。

进一步看DeprecatedTargetSdkVersionDialog的代码。

如果用户点击[ok],会改变一个参数AppWarnings.FLAG_HIDE_DEPRECATED_SDK的属性,也就是不会每次都检查这个属性。

public DeprecatedTargetSdkVersionDialog(final AppWarnings manager, Context context, ApplicationInfo appInfo) {
    // ...
    final AlertDialog.Builder builder = new AlertDialog.Builder(context)
                .setPositiveButton(R.string.ok, (dialog, which) ->
                    manager.setPackageFlag(
                            mPackageName, AppWarnings.FLAG_HIDE_DEPRECATED_SDK, true))
                .setMessage(message)
                .setTitle(label);
    // ...
}

解决方式

由于客户不喜欢这个弹框。因此需要去掉,方法如下:

  1. AppWarnings.FLAG_HIDE_DEPRECATED_SDK 默认改为true,也就是每次都跳过[没测过,推荐方式2]

  2. showDeprecatedTargetDialogIfNeeded()中注销弹出代码。[完美]

参考文章

  1. 应用弹窗“此应用专为旧版Android打造,因此可能无法正常运行…”的原因

相关文章

暂无评论

none
暂无评论...