Android 5.0 6.0 7.0 8.0 9.0 10.0 适配文件管理器应用安装

安装应用一般需要添加的权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.REPLACE_EXISTING_PACKAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

在AndroidManifest.xml文件中application中添加如下权限,该权限适用于Android 10.0

android:requestLegacyExternalStorage="true"
android:usesCleartextTraffic="true"

判断点击文件是否为apk,并且适配Android 5.0 到Android 10.0

导入包:import androidx.core.content.FileProvider;

else if(TypeFilter.getInstance().isApkFile(item_ext)){
   
   if(file.exists()) {
      if(mReturnIntent) {
         returnIntentResults(file);
         
      } else {
try {
   Intent installIntent = new Intent();
   installIntent.setAction(Intent.ACTION_VIEW);
   Uri apkUri = FileProvider.getUriForFile(getApplicationContext(), "com.softwinner.explore.fileprovider", file);///-----ide文件提供者名
   int version = android.os.Build.VERSION.SDK_INT;
   if (version > 24) {//android 7.0-10.0及以上版本
      installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      installIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
      installIntent.setDataAndType(apkUri, "application/vnd.android.package-archive");
   } else {//android 5.0-7.0
      installIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      installIntent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");

   }
   startActivity(installIntent);
}catch (IllegalArgumentException e){

}
      }
   }
}

在AndroidManifest.xml文件中添加provider

<provider
    android:authorities="com.softwinner.explore.fileprovider"
    android:name="androidx.core.content.FileProvider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/filepaths" />
</provider>

在res目录下面创建一个xml文件,在该文件中创建filepaths文件

<?xml version="1.0" encoding="utf-8"?>

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <root-path path="" name="cmrootfile" />
    <files-path path="" name="cmfiles" />
    <cache-path path="" name="cmcache" />
    <external-path path="" name="cmexternal" />
    <external-files-path path="" name="cmexternalfile" />
    <external-cache-path path="" name="cmexternalcache" />
</paths>

备注说明:
Android10 中使用FileProvider需要导入FileProvider包

import androidx.core.content.FileProvider;

在build.gradle文件中添加androidx的依赖

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值