我正在为 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);
}
};
请为我提供上述问题的解决方案。谢谢。