-2

I am developing an application for android which on downloading the upgrade prompts the user to install. But it shows "Parser error: There was a problem while parsing the package."after downloading the file successfully.

Following is my code to download the upgrade through download manager:-

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

}

And in the BroadcastReceiver it shows the popup to install the application.

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

Please provide me solution to above issue.Thank You.

  • 2
    *Please provide me solution to above issue.* please use google ... obviously there is something wrong with downloaded apk ... – Selvin Nov 18 '15 at 13:36
  • 1
    We don't do your homework. Check [How to ask](http://stackoverflow.com/help/how-to-ask) and reformat your question, please. – Mariano Zorrilla Nov 18 '15 at 14:01

1 Answers1

0

Thank you for your "helpful" comments,figured out the problem.The "downloadReference = downloadManager.enqueue(request);" passes download ID which should be checked in BroadcastReceiver

In startDownload(url) method:-

long downloadReference = downloadManager.enqueue(request);

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