2017-12-29 26 views
0

で放送意向を受けた私はdownloadManagerで、更新のための静的関数自分のアプリケーションを持っている:java.lang.RuntimeException:エラーがインストールAPK

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:放送インテントを受信中にエラーが発生しました{act = android.intent.action.DOWNLOAD_COMPLETE flg = 0x10 pkg = ...

スタックトレース:stack trace

答えて

1
manager.getMimeTypeForDownloadedFile(downloadId) 

これは、MIMEタイプとしてapplication/mobile戻っています。これは有効なMIMEタイプではありません。おそらく、DownloadManagerは、このコンテンツをダウンロードしたWebサーバーから取得しています。それがWebサーバーの場合は、コンテンツに適切なMIMEタイプを使用するように修正します。

+0

私はinstallIntent.setDataAndType(uri、 "application/vnd.android.package-archive")に変更しました。 – ingenious

関連する問題