【问题标题】:java.lang.RuntimeException: Error receiving broadcast Intent On install Apkjava.lang.RuntimeException:安装 Apk 时接收广播 Intent 时出错
【发布时间】:2018-06-10 02:09:55
【问题描述】:

我有一个使用 downloadManager 更新我的应用程序的静态函数:

 public static void downloadApk(final Context context, String apkurl){
        String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
        String fileName = "apk_update.apk";
        destination += fileName;
        final Uri uri = Uri.parse("file://" + destination);

        //Delete update file if exists
        File file = new File(destination);
        if (file.exists())
            file.delete();

        //set download manager
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkurl));
        request.setDescription("...");
        request.setTitle("...");

        //set destination
        request.setDestinationUri(uri);

        // get download service and enqueue file
        final DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        final long downloadId = manager.enqueue(request);

        //set BroadcastReceiver to install app when .apk is downloaded
        BroadcastReceiver onComplete = new BroadcastReceiver() {
            public void onReceive(Context ctxt, Intent intent) {
                Intent installIntent = new Intent(Intent.ACTION_VIEW);
                installIntent.setDataAndType(uri, manager.getMimeTypeForDownloadedFile(downloadId));
                installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
                ctxt.startActivity(installIntent); ////Error on here
                ctxt.unregisterReceiver(this);
               // ctxt.finish();
            }
        };
        //register receiver for when .apk download is compete
        context.registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }
}

在线下载complate后出现错误:

    ctxt.startActivity(installIntent);

我的错误描述:

java.lang.RuntimeException: 接收广播 Intent 时出错 { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=...

堆栈跟踪:stack trace

【问题讨论】:

    标签: android android-intent android-download-manager


    【解决方案1】:
    manager.getMimeTypeForDownloadedFile(downloadId)
    

    这将返回 application/mobile 作为 MIME 类型。这不是有效的 MIME 类型。据推测,DownloadManager 是从您下载此内容的 Web 服务器获得的。如果那是您的 Web 服务器,请将其修复为使用正确的 MIME 类型来处理您的内容。

    【讨论】:

    • 我改成 installIntent.setDataAndType(uri, "application/vnd.android.package-archive");
    猜你喜欢
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 2018-10-27
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多