简体   繁体   English

我想以编程方式从 Android 下载 apk

[英]I want to download apk from Android programmatically

I want to download apk from Android programmatically.我想以编程方式从 Android 下载 apk。 Apk file downloaded but I give There was a problem parsing the package message.下载了 Apk 文件,但我给出了解析 package 消息时出现问题。 I opening apk in download files, application is running.我在下载文件中打开 apk,应用程序正在运行。 I want the apk file to be installed directly without getting the error.我希望直接安装apk文件而不会出现错误。

public void Download(){

    Uri uri= Uri.parse(downloadUrl);

    DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    DownloadManager.Request request = new DownloadManager.Request(uri);
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
            DownloadManager.Request.NETWORK_MOBILE);

    request.setTitle(title);
    request.setDescription(description);

    //request.allowScanningByMediaScanner();
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

    //set the local destination for download file to a path within the application's external files directory
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,apkName);
    //request.setMimeType("*/*");
    request.setMimeType("application/vnd.android.package-archive");
    downloadManager.enqueue(request);
}

Try this尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM