Android 下載文件通知欄顯示進(jìn)度條功能的實(shí)例代碼
1、使用AsyncTask異步任務(wù)實(shí)現(xiàn),調(diào)用publishProgress()方法刷新進(jìn)度來實(shí)現(xiàn)(已優(yōu)化)
public class MyAsyncTask extends AsyncTask<String,Integer,Integer> { private Context context; private NotificationManager notificationManager; private NotificationCompat.Builder builder; public MyAsyncTask(Context context){ this.context = context; notificationManager = (NotificationManager) context.getSystemService(Activity.NOTIFICATION_SERVICE); builder = new NotificationCompat.Builder(context); } @Override protected void onPreExecute() { super.onPreExecute(); builder.setSmallIcon(R.mipmap.ic_launcher) .setContentInfo("下載中...") .setContentTitle("正在下載"); } @Override protected Integer doInBackground(String... params) { Log.e(TAG, "doInBackground: "+params[0] ); InputStream is = null; OutputStream os = null; HttpURLConnection connection = null; int total_length = 0; try { URL url1 = new URL(params[0]); connection = (HttpURLConnection) url1.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(50000); connection.connect(); if(connection.getResponseCode() == 200){ is = connection.getInputStream(); os = new FileOutputStream("/sdcard/zongzhi.apk"); byte [] buf = new byte[1024]; int len; int pro1=0; int pro2=0; // 獲取文件流大小,用于更新進(jìn)度 long file_length = connection.getContentLength(); while((len = is.read(buf))!=-1){ total_length += len; if(file_length>0) { pro1 = (int) ((total_length / (float) file_length) * 100);//傳遞進(jìn)度(注意順序) } if(pro1!=pro2) { // 調(diào)用update函數(shù),更新進(jìn)度 publishProgress(pro2=pro1); } os.write(buf, 0, len); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { if (is != null) { is.close(); } if (os != null) { os.close(); } } catch (IOException e) { e.printStackTrace(); } if (connection != null) { connection.disconnect(); } } return total_length; } @Override protected void onCancelled(Integer integer) { super.onCancelled(integer); } @Override protected void onCancelled() { super.onCancelled(); } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); builder.setProgress(100,values[0],false); notificationManager.notify(0x3,builder.build()); //下載進(jìn)度提示 builder.setContentText("下載"+values[0]+"%"); if(values[0]==100) { //下載完成后點(diǎn)擊安裝 Intent it = new Intent(Intent.ACTION_VIEW); it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); it.setDataAndType(Uri.parse("file:///sdcard/zongzhi.apk"), "application/vnd.android.package-archive"); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, it, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentTitle("下載完成") .setContentText("點(diǎn)擊安裝") .setContentInfo("下載完成") .setContentIntent(pendingIntent); notificationManager.notify(0x3, builder.build()); } } @Override protected void onPostExecute(Integer integer) { super.onPostExecute(integer); if(integer == 100) { Toast.makeText(context, "下載完成", Toast.LENGTH_SHORT).show(); } } }
2、使用系統(tǒng)服務(wù)來實(shí)現(xiàn)(不是特別推薦此方法)
//取得系統(tǒng)的下載服務(wù) DownloadManager downloadManager= (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); //創(chuàng)建下載請求對象 DownloadManager.Request request=new DownloadManager.Request(Uri.parse(downUrl)); request.setDestinationInExternalPublicDir("目錄","文件名"); request.setNotificationVisibility(DownloadManager.Request.NETWORK_MOBILE|DownloadManager.Request.NETWORK_WIFI); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); downloadManager.enqueue(request);
總結(jié)
以上所述是小編給大家介紹的Android 下載文件通知欄顯示進(jìn)度條功能的實(shí)例代碼,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!
相關(guān)文章
Android Studio使用小技巧:布局預(yù)覽時填充數(shù)據(jù)
這篇文章主要介紹了Android Studio使用小技巧:布局預(yù)覽時填充數(shù)據(jù),本文給出了代碼操作實(shí)例和效果圖例,需要的朋友可以參考下2015-05-05Android WebView交互傳遞json字符串并解析的方法
這篇文章主要給大家介紹了關(guān)于Android中WebView交互傳遞json字符串并解析的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-05-05Android編程實(shí)現(xiàn)在一個程序中啟動另一個程序的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)在一個程序中啟動另一個程序的方法,結(jié)合實(shí)例形式分析了Android通過ResolveInfo類來取得啟動Acitivty類名的方法來啟動另一個程序的方法,需要的朋友可以參考下2017-02-02Android Studio導(dǎo)入Eclipse項(xiàng)目時.so庫文件的解決方法
這篇文章主要介紹了Android Studio導(dǎo)入Eclipse項(xiàng)目時項(xiàng)目中".so"庫文件的解決方法,需要的朋友可以參考下2018-06-06Android實(shí)現(xiàn)藍(lán)牙(BlueTooth)設(shè)備檢測連接
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)藍(lán)牙(BlueTooth)設(shè)備檢測連接,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-11-11Android使用GridView實(shí)現(xiàn)橫向滾動效果
這篇文章主要為大家詳細(xì)介紹了Android使用GridView實(shí)現(xiàn)橫向滾動效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07android onTouchEvent處理機(jī)制總結(jié)(必看)
下面小編就為大家?guī)硪黄猘ndroid onTouchEvent處理機(jī)制總結(jié)(必看)小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04利用Jetpack?Compose復(fù)刻游戲Flappy?Bird
Flappy?Bird是13年紅極一時的小游戲,其簡單有趣的玩法和變態(tài)的難度形成了強(qiáng)烈反差,引發(fā)全球玩家競相把玩!本文將通過Jetpack?Compose復(fù)刻這一游戲,感興趣的小伙伴可以了解一下2022-02-02Android中RecyclerView實(shí)現(xiàn)多級折疊列表效果(二)
這篇文章主要給大家介紹了Android中RecyclerView實(shí)現(xiàn)多級折疊列表的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-05-05AndriodStudio使用listview實(shí)現(xiàn)簡單圖書管理
這篇文章主要為大家詳細(xì)介紹了AndriodStudio使用listview實(shí)現(xiàn)簡單圖書管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03