調用系統下載服務下載文件。監聽下載完成拿到downloadid和filename


public class DownLoadAPK {
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static long downloadAPK(DownloadManager downloadManager, String apkUrl, String name, String desc){


        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(apkUrl));
        request.setDestinationInExternalPublicDir("zhnet", name+".apk");//表示設置下載地址為sd卡的Trinea文件夾,文件名為MeiLiShuo.apk。
        request.setTitle(name);//設置下載中通知欄提示的標題
        request.setDescription(desc);//設置下載中通知欄提示的介紹
        request.setVisibleInDownloadsUi(true);  //設置顯示下載界面
        request.setMimeType("application/vnd.android.package-archive");
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);//表示下載進行中和下載完成的通知欄是否顯示。
        // 默認只顯示下載中通知。
        // VISIBILITY_VISIBLE_NOTIFY_COMPLETED表示下載完成后顯示通知欄提示。VISIBILITY_HIDDEN表示不顯示任何通知欄提示,
        // 這個需要在AndroidMainfest中添加權限android.permission.DOWNLOAD_WITHOUT_NOTIFICATION.

//        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);//表示下載允許的網絡類型,默認在任何網絡下都允許下載。
        //有NETWORK_MOBILE、NETWORK_WIFI、NETWORK_BLUETOOTH三種及其組合可供選擇。
        //如果只允許wifi下載,而當前網絡為3g,則下載會等待。

//        request.setAllowedOverRoaming(true);//移動網絡情況下是否允許漫游。

//        request.setMimeType("application/cn.trinea.download.file");//設置下載文件的mineType。
        // 因為下載管理Ui中點擊某個已下載完成文件及下載完成點擊通知欄提示都會根據mimeType去打開文件,所以我們可以利用這個屬性。
        // 比如上面設置了mimeType為application/cn.trinea.download.file,
        // 我們可以同時設置某個Activity的intent-filter為application/cn.trinea.download.file,用於響應點擊的打開文件。

//        request.allowScanningByMediaScanner();//表示允許MediaScanner掃描到這個文件,默認不允許。

        //request.addRequestHeader(String header, String value)
        //添加請求下載的網絡鏈接的http頭,比如User-Agent,gzip壓縮等

        return downloadManager.enqueue(request);
    }
}

上面的工具類有詳細注釋說明,暫時搜集了這么些功能,下面是監聽系統下載完成系統發送廣播,監聽廣播獲取下載的詳細地址加文件名和id

public class DownloadReceiver extends BroadcastReceiver {
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public void onReceive(Context context, Intent intent) {
        long completeDownloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);

        Log.d("=====", "下載的IDonReceive: "+completeDownloadId);

        DownloadManager manager = (DownloadManager)context.getSystemService(Context.DOWNLOAD_SERVICE);
        if(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())){
            DownloadManager.Query query = new DownloadManager.Query();
            //在廣播中取出下載任務的id
            long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
            query.setFilterById(id);
            Cursor c = manager.query(query);
            if(c.moveToFirst()) {
                //獲取文件下載路徑
                String filename = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
                //如果文件名不為空,說明已經存在了,拿到文件名想干嘛都好
                if(filename != null){
                    Log.d("=====", "下載完成的文件名為:"+filename);
                    //     /storage/emulated/0/zhnet/T台魅影.apk

                    //執行安裝
                    Intent intent_ins = new Intent(Intent.ACTION_VIEW);
                    intent_ins.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent_ins.setDataAndType(Uri.parse("file://" + filename),"application/vnd.android.package-archive");
                    context.getApplicationContext().startActivity(intent_ins);
//                    filename = filename.substring(filename.lastIndexOf("/")+1, filename.lastIndexOf("."));
//                    Log.d("=====", "截取后的文件名onReceive: "+filename);
                }
            }
        }else if(DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(intent.getAction())){
            long[] ids = intent.getLongArrayExtra(DownloadManager.EXTRA_NOTIFICATION_CLICK_DOWNLOAD_IDS);
            //點擊通知欄取消下載
//            manager.remove(ids);
//            Toast.makeText(context, "已經取消下載", Toast.LENGTH_SHORT).show();

        }
    }
}

最后記得改清單文件

        <!--監聽系統下載完成的廣播-->
        <receiver android:name="com.zhnet.AD.broadcast.DownloadReceiver">
            <intent-filter>
                <action android:name="DownloadManager.ACTION_DOWNLOAD_COMPLETE"/>
                <action android:name="android.intent.action.DOWNLOAD_COMPLETE" />
                <action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED"/>
            </intent-filter>
        </receiver>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM