【发布时间】:2015-07-20 15:56:24
【问题描述】:
问题是,在我使用这种方法从服务器下载 .apk 文件后:
public void Update(String apkName) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
File file = new File("/sdcard/Download/"+ apkName + ".apk");
if (file.exists()) {
file.delete();
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS + "/", apkName + ".apk");
DownloadManager manager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
}
然后我使用 BroadCastReciever 在下载操作完成后执行安装操作,如下所示:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// some codes here...
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)){
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/Download/" + apkName + ".apk")),
"application/vnd.android.package-archive");
startActivity(i);
} else {
}
}
};
getActivity().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
download_apk(apkName);
return rootView;
}
现在的问题是,当下载操作完成时,DownloadManager 尝试安装 apk 文件,我得到 this,即“解析错误/解析包时出现问题”。
点击确定后,会出现真正的安装页面(由广播接收器触发)。
我在 Activity 中运行这个过程,它非常好。正如你在上面看到的,这次它是在 Fragment 中!这可能是原因吗??
如何阻止 DownloadManager 自动运行 apk? 谁能帮帮我?
【问题讨论】:
-
解析错误意味着检查你的
minSdk和TargetSdk版本 -
@MD:我做到了,没问题。我的设备是 kitkat 4.4,minSDK 是 13
-
targetsdk是什么?
标签: android broadcastreceiver download-manager