zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Android报错: exposed beyond app through Intent.getData()

AndroidApp 报错 Intent Through Beyond
2023-09-14 09:04:24 时间

AndroidManifest.xml

在<application 里添加

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

res/xml/fileproviderpath

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <root-path name="root" path="."></root-path>
    <files-path
        name="file"
        path="."></files-path>
    <files-path
        name="files"
        path="."></files-path>
    <cache-path
        name="cache"
        path="."></cache-path>
    <external-path
        name="external"
        path="."></external-path>
    <external-cache-path
        name="external_cache"
        path="."></external-cache-path>
    <external-files-path
        name="external_file"
        path="."></external-files-path>
    <external-path name="external_files" path="."/>
</paths>

安装APK代码

String path = "/sdcard/youku.apk";
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.putExtra("ISSILENT",1);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        Uri uri;
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            uri = FileProvider.getUriForFile(this,"com.example.silentinstallation.fileprovider",new File(path));
            intent.setDataAndType(uri,"application/vnd.android.package-archive");
        }else{
            uri = Uri.parse("file://" + path);
        }
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        startActivity(intent);
    }