-2

我正在为 android 开发一个应用程序,它在下载升级时会提示用户安装。但它显示“解析器错误:解析包时出现问题。”下载文件成功后。

以下是我通过下载管理器下载升级的代码:-

public void startDownload(String url)
{
    filename = url.substring(url.lastIndexOf("/") + 1, url.length());
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
    Log.i("Downloaded filename", filename);     request.setDestinationInExternalPublicDir(Environment.getExternalStorageState(), "Upgrade Download/"+ filename);
    Log.i("Download Path", Environment.getExternalStorageState() + "/Upgrade Download/" + filename);        
    request.allowScanningByMediaScanner();  
    request.setMimeType("application/vnd.android.package-archive");     request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    // Start download
    DownloadManager dm = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
    dm.enqueue(request);
    IntentFilter intentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);

    activity.registerReceiver(onComplete, intentFilter);

}

在 BroadcastReceiver 中,它显示了安装应用程序的弹出窗口。

广播接收器:-

BroadcastReceiver onComplete = new BroadcastReceiver()
{
    public void onReceive(Context ctxt, Intent intent)
    {


        File file = new File(Environment.getExternalStorageState() + "/Upgrade Download", filename);

        Log.i("open filename",""+ file.getPath());
        Log.i("intent",""+ intent);
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ctxt.startActivity(i);
    }
};

请为我提供上述问题的解决方案。谢谢。

4

1 回答 1

0

感谢您的“有用”评论,解决了问题。“downloadReference = downloadManager.enqueue(request);” 传递应在 BroadcastReceiver 中检查的下载 ID

在 startDownload(url) 方法中:-

long downloadReference = downloadManager.enqueue(request);

在广播接收器中:-

public void onReceive(Context ctxt, Intent intent)
{
        long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
        if (downloadReference == referenceId)
        {
          DownloadManager downloadManager = (DownloadManager) ctxt.getSystemService(Activity.DOWNLOAD_SERVICE);
            Intent installIntent = new Intent(Intent.ACTION_VIEW);        
           installIntent.setDataAndType(downloadManager.getUriForDownloadedFile(referenceId),
                    "application/vnd.android.package-archive");
            installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ctxt.startActivity(i);
        }
}
于 2015-12-02T07:29:01.210 回答