android 8.0 Apk更新事项

1.添加未知来源的权限判断;

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
alertView = new AlertView("温馨提示", "请前往设置中开启安装应用所需的权限?", "取消", new String[]{"确定"}, null, this,
        AlertView.Style.Alert, new OnItemClickListener() {
    @Override
    public void onItemClick(Object o, int position) {
        if (position != AlertView.CANCELPOSITION) {
            Uri packageURI = Uri.parse("package:" + getPackageName());
            Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
            startActivityForResult(intent, PACKAGES_RESULT);
            alertView.dismiss();
        }
    }
}).setOnDismissListener(new OnDismissListener() {
    @Override
    public void onDismiss(Object o) {

    }
});
alertView.show();

2.下载apk,注意下载apk的存放地址:

downloadFile = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "demo.apk");

3.下载完成后apk,安装:

try {
    String[] command = {"chmod", "777", downloadFile.getAbsolutePath()};
    ProcessBuilder builder = new ProcessBuilder(command);
    builder.start();
} catch (Exception ignored) {
    ignored.printStackTrace();
}
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //android N的权限问题
    Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", downloadFile);//demo.apk是我们下载的apk的文件文成
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
    intent.setDataAndType(Uri.fromFile(downloadFile), "application/vnd.android.package-archive");//demo.apk的文件文成
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
activity.startActivity(intent);

注意几个地方:

1.配置文件共享

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path
        name="download"
        path="" />
  </paths>
<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.siyuzh.sywireless.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

2.文件的下载地址

downloadFile = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "demo.apk");

3.7.0以上的安装

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值