繁体   English   中英

打算安装APK文件-Android牛轧糖

[英]Intent to install APK file - Android Nougat

我进行了广泛搜索,但没有发现问题。 当您尝试在Android Nougat使用Intent安装APK file ,它根本不会安装,并显示以下警告:“解析软件包时出现问题”。

例如,使用打开这种类型的文件( PDF )的设置,它可以完美地打开PDF文件。 但是要安装。 apk文件不起作用。

LogCat没有显示任何错误,我LogCat任何问题。

有什么事吗

如下代码:

清单

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />

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

xml /文件路径:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="storage/emulated/0" path="."/>

活动:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        try {
            Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
            File file = new File(getContext().getFilesDir(), "app-debug.apk");
            Uri uri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", file);

            intent.setDataAndType(uri, "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(intent);
            finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }else{
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            File file = new File(Environment.getExternalStorageDirectory() + "/app-debug.apk");

            intent.setAction(android.content.Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(intent);
            finish();
        } catch (Exception e) {
            e.getMessage();
        }
    }

拜托,这段代码可能出什么问题了? 有人能帮我吗?

我必须更改N(或更高)的意图,并删除类型名称。 一旦完成,安装将按预期进行。

所以对于N:

Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
File file = new File(getContext().getFilesDir(), "app-debug.apk");
Uri uri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", file);
intent.setData(uri)
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
finish();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM