从url下载APK并执行

时间:2015-01-07 15:00:38

标签: android apk android-download-manager

我使用DownloadManager从网址下载apk。下载完成后,我在我的BroadcastReceiver中获取了DownloadManager.ACTION_DOWNLOAD_COMPLETE的onReceive。

一些编码:我将apk文件从url下载到下载目录。

DownloadManager.Request r = new DownloadManager.Request(mUri);
r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "myapp.apk");
r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager dm = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
SharedPreferences mSharedPref = activity.getSharedPreferences("package", Context.MODE_PRIVATE);
mSharedPref.edit().putLong("downloadID", dm.enqueue(r)).commit();

的onReceive

File apkFile = new File(Environment.DIRECTORY_DOWNLOADS + "myapp.apk");
Intent promptInstall = new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
startActivity(promptInstall);

问题:

  • 文件在那里,我想执行它但是我没有显示包安装程序
  • 即使我在手机下载文件夹中看到该文件,如果我想安装它,也没有选择包安装程序
  • 我不确定我是否正确处理文件和路径..
  • 当我尝试打开文件
  • 时,我从android获得了解析错误

有人有个主意吗?

3 个答案:

答案 0 :(得分:1)

我最近不得不做这样的事情,希望它有所帮助:

编辑:几个笔记, apkurl是下载位置的字符串。 使字节缓冲区足够大以便您的响应

    try {

        String PATH = Environment.getExternalStorageDirectory() + "/download/";
        File file = new File(PATH);
        file.mkdirs();
        // Create a file on the external storage under download
        File outputFile = new File(file, "app.apk");
        FileOutputStream fos = new FileOutputStream(outputFile);

        HttpGet m_httpGet = null;
        HttpResponse m_httpResponse = null;

        // Create a http client with the parameters
        HttpClient m_httpClient = setupHttpClient();
        String result = null;

        try {

            // Create a get object
            m_httpGet = new HttpGet(apkurl);

            // Execute the html request
            m_httpResponse = m_httpClient.execute(m_httpGet);
            HttpEntity entity = m_httpResponse.getEntity();

            // See if we get a response
            if (entity != null) {

                InputStream instream = entity.getContent();
                byte[] buffer = new byte[1024];

                // Write out the file
                int len1 = 0;
                while ((len1 = instream.read(buffer)) != -1) {
                    fos.write(buffer, 0, len1);
                }
                fos.close();
                instream.close();// till here, it works fine - .apk is download to my sdcard in download file

            }

        } catch (ConnectTimeoutException cte) {
            // Toast.makeText(MainApplication.m_context, "Connection Timeout", Toast.LENGTH_SHORT).show();
            return false;
        } catch (Exception e) {
            return false;
        } finally {
            m_httpClient.getConnectionManager().closeExpiredConnections();
        }

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(
                Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk")),
                "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        MainApplication.getApp().getApplicationContext().startActivity(intent);

        // System.exit(0);

    } catch (IOException e) {
        Debug.ERROR(CLASSNAME, METHODNAME, "Failed to update new apk");
        return false;
    } catch (Exception e1) {
        Debug.ERROR(CLASSNAME, METHODNAME, "Failed to update new apk");
        return false;
    }

    return true;

答案 1 :(得分:0)

确保您在android.permission.INSTALL_PACKAGES中声明了AndroidManifest.xml权限。

然后获取您的APK路径:

File apkFile = new File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk");

现在运行意图:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

答案 2 :(得分:0)

您使用该设备进行的所有下载...

通过“下载”澄清我的意思,这里是截图。这是Nexus 4上针对Android 5的标准下载文件夹。当我点击其中的apk时,我没有得到安装.apk的提示。相反,HTML-Viewer或其他无用的东西显示出来选择......

一个可能的错误可能是DownloadManager ..也许他“标记”下载的文件错误,因此它不被解释为apk文件,我不知道......但我打电话

promptInstall.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/myapp.apk")), "application/vnd.android.package-archive");

all downloads... inside..