"解析器错误"在android下载升级后

时间:2015-11-18 13:31:41

标签: android parsing android-download-manager android-broadcastreceiver packageinstaller

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

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

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);
    }
};

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

1 个答案:

答案 0 :(得分:0)

感谢您的帮助"评论,找出问题。" downloadReference = downloadManager.enqueue(request);"传递下载ID,应在BroadcastReceiver中检查

在startDownload(url)方法中: -

long downloadReference = downloadManager.enqueue(request);

在BroadcastReceiver中: -

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);
        }
}