Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

安卓8.0 安装闪退的bug及解决方案 #51

Open
aerke-dev opened this Issue Dec 24, 2017 · 6 comments

Comments

Projects
None yet
3 participants
@aerke-dev
Copy link

aerke-dev commented Dec 24, 2017

在使用本项目做App版本更新时(应用上线半年多了,一直在使用本项目),使用Android 8.0的手机测试发现一个问题:安装包下载完成之后,屏幕闪了一下并没有跳转到安装界面,使用8.0以下的手机并没有这个问题。
原因:Android 8.0 Oreo 中,Google 移除掉了容易被滥用的“允许位置来源”应用的开关,在安装 Play Store 之外的第三方来源的 Android 应用的时候,竟然没有了“允许未知来源”的检查框,如果你还是想要安装某个被自己所信任的开发者的 app,则需要在每一次都手动授予“安装未知应用”的许可。
适配Android 8.0:
1.清单文件添加安装未知来源应用的权限 android.permission.REQUEST_INSTALL_PACKAGES

2.下载好安装包后,安装程序之前验证是否有安装未知来源应用的权限
public class MyActivity extends Activity {

private static final int REQUEST_INSTALL = 124;
private final UpdateBuilder builder = UpdateBuilder.create();

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	/**
	 * 请在Application中进行UpdateConfig的全局配置
	 */
	builder.setInstallStrategy((context, s, update) -> {
        if (Build.VERSION.SDK_INT < 26) {
                //直接安装
                new DefaultInstallStrategy().install(context, s, update);
            } else if (getPackageManager().canRequestPackageInstalls()) {//26 版本才有此方法
                //可以安装未知来源应用
                new DefaultInstallStrategy().install(context, s, update);
            } else {
                //申请权限
                if (ActivityCompat.shouldShowRequestPermissionRationale(MyActivity.this, Manifest.permission.REQUEST_INSTALL_PACKAGES)) {
					//自定义的Dialog,可以用Android系统自带Dialog代替
                    new MessageDialog.Builder(UMainActivity.this)
                            .title("提示")
                            .content("为了正常升级“预逍”APP,请允许“预逍”安装未知来源应用,本功能只限用于版本升级")
                            .positiveText("确定")
                            .negativeText("取消")
                            .canceledOnTouchOutside(false)
                            .cancelable(false)
                            .onPositive((dialog, which) -> ActivityCompat.requestPermissions(MyActivity.this,
                                    new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES},
                                    REQUEST_INSTALL))
                            .onNegative(((dialog, which) -> ActivityCompat.requestPermissions(MyActivity.this,
                                    new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES},
                                    REQUEST_INSTALL)))
                            .show();
                } else {
                    ActivityCompat.requestPermissions(MyActivity.this,
                            new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES},
                            REQUEST_INSTALL);
                }
            }
    });
     builder.check();
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_INSTALL) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            builder.check();
        } else {
			//启动授权页面
            Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, REQUEST_INSTALL);
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_INSTALL) {
        builder.check();
    }
}

}
@yjfnypeu 建议InstallStrategy新增权限申请回调

@yjfnypeu

This comment has been minimized.

Copy link
Owner

yjfnypeu commented Dec 25, 2017

好的。多谢提供资料!已加入更新计划中。

@yjfnypeu

This comment has been minimized.

Copy link
Owner

yjfnypeu commented Dec 28, 2017

呃。。。我这没有8.0的机型。使用8.0的模拟器进行了测试。测试启动安装程序不受影响的。请问还有需要别的什么条件么?

@aerke-dev

This comment has been minimized.

Copy link
Author

aerke-dev commented Dec 28, 2017

@yjfnypeu 就需要两个条件:
1.清单文件添加安装未知来源应用的权限 android.permission.REQUEST_INSTALL_PACKAGES
2.启动安装之前确保有android.permission.REQUEST_INSTALL_PACKAGES权限(最晚在执行InstallStrategy().install(context, s, update)之前获取权限)
备注:该权限跟其他权限一样,在没有获取权限的情况下执行安装操作会抛异常。

@yjfnypeu

This comment has been minimized.

Copy link
Owner

yjfnypeu commented Dec 29, 2017

@aerke-dev 我是指的是会抛异常的配置环境。因为我这边不这样配置。直接使用也没见抛异常,也正常升级了。没有具体错误环境我这边不好进行修改测试。

@yjfnypeu

This comment has been minimized.

Copy link
Owner

yjfnypeu commented Dec 29, 2017

@aerke-dev 刚和一些朋友商量了一下。觉得还是不在框架内接入动态权限请求了。现在已经有很成熟的动态权限申请框架了。在启动前先进行权限申请就好。

@wentulu

This comment has been minimized.

Copy link

wentulu commented Jul 27, 2018

@aerke-dev 在进入未知来源设置页面后,按钮点击无反应,三星s8和华为6p

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.