android - 从url下载APK并执行

标签 android apk android-download-manager

我正在使用 DownloadManager 从 url 下载 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();

接收时

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 的解析错误

有人有想法吗?

最佳答案

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

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

关于android - 从url下载APK并执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27822218/

相关文章:

android - 使用 Android Pie 时下载失败并出现错误 400

java - 通过 Unicode 字符或其他方式在 Android TextView 中显示情感图标

java - android.app.Activity.java 中的错误

android - 保留apk到底是做什么的

Firebase Phone Auth 在 APK Release模式下不工作

android - 在哪里可以找到 DownloadManager 的 ContentProvider 实现

java - picasso 图像无法加载

android - 我一直试图在Kotlin中使用For Loop来过滤此JsonObject

android - XAPK 文件验证失败 - APK 扩展文件

android - DownloadManager ERROR_CANNOT_RESUME,状态失败