Android:为什么Download-Manager会在android中自动运行(下载)apk文件?

时间:2015-07-15 12:28:44

标签: android broadcastreceiver download-manager

问题是,在我使用此方法从服务器下载.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,这是“解析错误/解析包时出现问题”。

单击“确定”后,显示“真正的安装页面”(由broadcastReciever触发)。

我在Activity中运行此进程,这非常好。如上所述,这次是碎片!这可能是原因??

如何从自动运行apk停止DownloadManager? 有人可以帮帮我吗?

0 个答案:

没有答案