如何打开在应用程序中下载的应用程序(修复代码)?

时间:2019-07-16 00:51:32

标签: java android flutter

这是用于打开和安装已下载应用程序的代码;此代码将打开我制作的应用程序。它不起作用或无法启动应用程序,并导致错误。

      String action = intent.getAction();
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
            intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0L);
            DownloadManager.Query query = new DownloadManager.Query();
            Cursor cursor = downloadManager.query(query);
            if (cursor.moveToFirst()) {
                int i = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
                if (DownloadManager.STATUS_SUCCESSFUL == cursor.getInt(i)) {
                    String uri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

                    File file = new File(uri);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        Uri apkUri = FileProvider.getUriForFile(context,
                                "com.sysvoid.aromapark.provider", file);
                        Intent intentOne = new Intent(Intent.ACTION_INSTALL_PACKAGE);
                        intent.setData(apkUri);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                                Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        activity.startActivity(intentOne);
                    } else {
                        Intent install = new Intent(Intent.ACTION_VIEW);
                        install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                                Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        install.setDataAndType(Uri.parse(uri),
                                "application/vnd.android.package-archive");
                        activity.startActivity(install);
                    }
                }
            }
        }

这是XML路径:

<?xml version="1.0" encoding="utf-8"?>
<Paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="Download" path="Download/" />
</Paths>

这是提供者:

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

0 个答案:

没有答案