根据google官网得知,在8.0以上权限控制的更加严格,应用内安装下载更新的apk都需要申请“安装外面应用”权限才能去安装新应用,如果没有申请否则无法安装。
顺便附上6.0、7.0设备解决方案:
一、设备6.0
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
private fun requestPermissions() {
val permissionsList = arrayListOf(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
)
if (ContextCompat.checkSelfPermission(this, permissionsList[0])!= PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, permissionsList[1])!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, permissionsList.toTypedArray(), 100)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (grantResults.isNotEmpty() && requestCode == 100) {
//授权后操作哦
} else {
Toast.makeText(this, "请开启相关权限!", Toast.LENGTH_LONG).show()
}
}
二、设备7.0 ,在以上代码上新增如下:
1.需要在AndroidManifest.xml中配置FileProvider
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="{包名}.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
2.xml目录下新建file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<root-path
name="external_storage_root"
path="." />
<root-path
name="camera_photos"
path="." />
<root-path
name="files_root"
path="." />
<root-path
name="files_root"
path="." />
<root-path
name="external_files"
path="." />
</paths>
三、设备8.0+
1.在 AndroidManifest.xml新增权限:
<!--android O更新了未知来源的权限-->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
四、 打开安装包的代码
public class InstallUtil {
private Activity mAct;
private String mPath;//下载下来后文件的路径
public static int UNKNOWN_CODE = 2018;
public InstallUtil(Activity mAct, String mPath) {
this.mAct = mAct;
this.mPath = mPath;
}
public void install() {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startInstallO();
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
startInstallN();
} else {
startInstall();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* android1.x-6.x
*/
public void startInstall() throws Exception {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.parse("file://" + mPath), "application/vnd.android.package-archive");
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mAct.startActivity(install);
}
/**
* android7.x
*/
public void startInstallN() throws Exception {
//参数1 上下文, 参数2 在AndroidManifest中的android:authorities值, 参数3 共享的文件
Uri apkUri = FileProvider.getUriForFile(mAct, getAuthority(mAct, ".FileProvider"), new File(mPath));
Intent install = new Intent(Intent.ACTION_VIEW);
//由于没有在Activity环境下启动Activity,设置下面的标签
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//添加这一句表示对目标应用临时授权该Uri所代表的文件
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
install.setDataAndType(apkUri, "application/vnd.android.package-archive");
mAct.startActivity(install);
}
/**
* android8.x
*/
@RequiresApi(api = Build.VERSION_CODES.O)
private void startInstallO() throws Exception {
boolean isGranted = mAct.getPackageManager().canRequestPackageInstalls();
if (isGranted) {
startInstallN();//安装应用的逻辑(写自己的就可以)
} else {
new AlertDialog.Builder(mAct)
.setCancelable(false)
.setTitle("安装应用需要打开未知来源权限,请去设置中开启权限")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface d, int w) {
//https://blog.csdn.net/changmu175/article/details/78906829
Uri packageURI = Uri.parse("package:" + mAct.getPackageName());
//注意这个是8.0新API
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
mAct.startActivityForResult(intent, INSTALL_PERMISS_CODE);
}
})
.show();
}
}
/**
* 获取FileProvider
* 返回: "此处为你的包名.FileProvider"
* china.test.provider
*/
private String getAuthority(Context context, String authority) {
return getAppProcessName(context) + authority;
}
/**
* 获取当前应用程序的包名
*
* @param context 上下文对象
* @return 返回包名
*/
private String getAppProcessName(Context context) {
//当前应用pid
int pid = android.os.Process.myPid();
//任务管理类
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//遍历所有应用
List<ActivityManager.RunningAppProcessInfo> infos = manager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo info : infos) {
if (info.pid == pid)//得到当前应用
{
return info.processName;//返回包名
}
}
return "";
}
}
五、运行后发现还是有问题、安装失败
在app.gradle中添加signingConfigs配置,这样在不打包的前提下也可以安装成功啦!
android {
defaultConfig { … }
signingConfigs {
debug {
storeFile file("./xxx.keystore")
storePassword 'password'
keyAlias 'xxx'
keyPassword 'password'
}
release {
storeFile file("./xxx.keystore")
storePassword 'password'
keyAlias 'xxx'
keyPassword 'password'
v2SigningEnabled false
}
}
参考文献:
android 6.0以上系统部分需要动态授权代码_一直向钱的博客-CSDN博客
Android8.0+应用内更新安装apk失败_fplei的博客-CSDN博客
android 7.0、8.0、9.0适配时遇到更新安装问题解决_Bentley_li的博客-CSDN博客
Android8.0 允许安装未知来源权限(一)_飞奔的小付的博客-CSDN博客
记 Android 7.0 8.0版本更新安装遇到的坑 - 简书
android 调用系统播放器播放视频,适配android 7.0以上_书中有颜如玉的博客-CSDN博客_android 调用系统播放器