国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android 下載文件通知欄顯示進(jìn)度條功能的實(shí)例代碼

2019-12-12 01:00:16
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

1、使用AsyncTask異步任務(wù)實(shí)現(xiàn),調(diào)用publishProgress()方法刷新進(jìn)度來(lái)實(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ù)來(lái)實(shí)現(xiàn)(不是特別推薦此方法)

//取得系統(tǒng)的下載服務(wù)    DownloadManager downloadManager= (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);    //創(chuàng)建下載請(qǐng)求對(duì)象    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í)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 治县。| 邵阳县| 栾城县| 阿巴嘎旗| 尚义县| 临海市| 康保县| 敦化市| 新营市| 宜章县| 绵竹市| 静安区| 瑞金市| 曲周县| 彩票| 阿坝| 潮安县| 黄骅市| 曲靖市| 温泉县| 洛南县| 开封县| 翁源县| 武陟县| 郎溪县| 庆元县| 怀仁县| 清河县| 晋宁县| 台湾省| 周口市| 定结县| 廊坊市| 务川| 拉孜县| 庆阳市| 南开区| 同江市| 平武县| 泾源县| 马龙县|