簡體   English   中英

我想以編程方式從 Android 下載 apk

[英]I want to download apk from Android programmatically

我想以編程方式從 Android 下載 apk。 下載了 Apk 文件,但我給出了解析 package 消息時出現問題。 我在下載文件中打開 apk,應用程序正在運行。 我希望直接安裝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);
}

嘗試這個

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