Android8.0版本升级适配

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ayrascal/article/details/81095335

1.在AndroidManifest.xml加入权限

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

2.在/src/res/xml下新建file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <paths>
        <external-path path ="" name = "download"/>
    </paths>
</resources>

3.在AndroidManifest.xml中注册

       <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="包名.fileprovider"
            android:grantUriPermissions="true"
            android:exported="false">
            <!--元数据-->
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

4.在显示下载框之前新增

int permission = ActivityCompat.checkSelfPermission(mContext,
								Manifest.permission.WRITE_EXTERNAL_STORAGE);

						if (permission != PackageManager.PERMISSION_GRANTED) {
							// We don't have permission so prompt the user
							ActivityCompat.requestPermissions((Activity)mContext, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},10010);
						}

修改安装apk代码

private void installApk() {
		File apkfile = new File(mSavePath, mHashMap.get("name"));
		if (!apkfile.exists()) {
			return;
		}
		
		Uri apkUri =
				FileProvider.getUriForFile(mContext, "包名.fileprovider", apkfile);

		Intent intent = new Intent(Intent.ACTION_VIEW);
		// 由于没有在Activity环境下启动Activity,设置下面的标签
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		//添加这一句表示对目标应用临时授权该Uri所代表的文件
		intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
		intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
		mContext.startActivity(intent);
	}

版本更新代码可参考 https://blog.csdn.net/ayrascal/article/details/70889307

demo下载链接:https://download.csdn.net/download/ayrascal/10672994

 

 

展开阅读全文

没有更多推荐了,返回首页