从 url 下载 APK 并执行

Download APK from url and execute

我正在使用 DownloadManager 从 url 下载 apk。下载完成,我在我的 BroadcastReceiver 中获得了 DownloadManager.ACTION_DOWNLOAD_COMPLETE.

的 onReceive

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

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

接收

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

问题:

有人有想法吗?

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

然后获取您的 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);

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

编辑:一些笔记, 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;

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

为了阐明我在 "Downloads" 中的意思,这里有一个屏幕截图。这是 Nexus 4 上 Android 5 的标准下载文件夹。当我单击其中的 apk 时,我没有收到安装 .apk 的提示。相反 HTML-Viewer 或其他无用的东西会出现以供选择...

一个可能的错误可能是 DownloadManager.. 也许他 "tags" 下载的文件有误,所以它没有被解释为 apk 文件,我不知道...但我调用

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