调用系统安装应用

/**
 * 调用系统安装应用
 */
public static boolean installApk(Context context, File file) {
    if (file == null || !file.exists() || !file.isFile()) {
        return false;
    }
    try {
        Uri uri;
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            //设置7.0以上共享文件,分享路径定义在xml/file_paths.xml
            //String authorities = "${applicationId}.fileprovider";
            //String authorities = "com.xx.android.xx.xx.fileprovider";
            //uri = FileProvider.getUriForFile(context, authorities, file);
            uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", file);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            // 7.0以下,共享文件
            uri = Uri.fromFile(file);
        }

        intent.setDataAndType(uri,
                "application/vnd.android.package-archive");

        context.startActivity(intent);
    }catch (Exception e){

    }
    return true;
}

 

文件操作权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <paths>
        <!-- Context.getFilesDir() + "/path/" -->
        <files-path
            name="my_files"
            path="xx/"/>
        <!-- Context.getCacheDir() + "/path/" -->
        <cache-path
            name="my_cache"
            path="xx/"/>
        <!-- Context.getExternalFilesDir(null) + "/path/" -->
        <external-files-path
            name="external-files-path"
            path="xx/"/>
        <!-- Context.getExternalCacheDir() + "/path/" -->
        <external-cache-path
            name="name"
            path="xx/" />
        <!-- Environment.getExternalStorageDirectory() + "/path/" -->
        <external-path
            name="my_external_path"
            path="xx/"/>
        <!-- Environment.getExternalStorageDirectory() + "/path/" -->
        <external-path
            name="external_storage_root"
            path="."/>

        <external-path
            name="download"
            path="" />

        <external-path
            name="external_path"
            path="xx/download/" />

        <external-path
            name="external_path"
            path="xx/" />
    </paths>
</resources>

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:networkSecurityConfig="@xml/network_config"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:name=".App">
      
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true"
            tools:replace="android:authorities"
            >
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

      

    </application>

 

network_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值