Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android實現Service下載文件,Notification顯示下載進度的示例

Android實現Service下載文件,Notification顯示下載進度的示例

編輯:關於Android編程

先放個gif。。最終效果如果:

主要演示了Android從服務器下載文件,調用Notification顯示下載進度,並且在下載完畢以後點擊通知會跳轉到安裝APK的界面,演示是在真實的網絡環境中使用真實的URL進行演示,來看看代碼:

MainActivity代碼非常簡單,就是啟動一個Service:

public class MainActivity extends AppCompatActivity {
 String download_url="http://shouji.360tpcdn.com/160329/a9037075b8d3aa98fbf6115c54a5b895/com.alensw.PicFolder_4722404.apk";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

 }
 public void bt_start_service(View view){
  Intent intent=new Intent(this,DownLoadService.class);
  intent.putExtra("download_url",download_url);
  startService(intent);
 }
}

DownLoadService裡面,在onStartCommand方法裡面是關鍵代碼,調用NotifyUtil這個工具類的“notify_progress”方法去顯示一個通知,與此同時開始下載APK文件,DownLoadService代碼如下:

public class DownLoadService extends Service {
 String download_url;
 String savePath= Environment.getExternalStorageDirectory()+"/liulan.apk";
 private int requestCode = (int) SystemClock.uptimeMillis();
 private NotifyUtil currentNotify;
 File mFile;
 @Nullable
 @Override
 public IBinder onBind(Intent intent) {
  return null;
 }

 @Override
 public void onCreate() {
  super.onCreate();


 }

 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
  mFile=new File(savePath);
  download_url=intent.getStringExtra("download_url");
  Log.e("test","執行onStartCommand");
  //設置想要展示的數據內容
  Intent intent_noti = new Intent();
  intent_noti.setAction(Intent.ACTION_VIEW);
  //文件的類型,從tomcat裡面找
  intent_noti.setDataAndType(Uri.fromFile(mFile), "application/vnd.android.package-archive");
  PendingIntent rightPendIntent = PendingIntent.getActivity(this,
    requestCode, intent_noti, PendingIntent.FLAG_UPDATE_CURRENT);
  int smallIcon = R.drawable.xc_smaillicon;
  String ticker = "正在更新快圖浏覽";
  //實例化工具類,並且調用接口
  NotifyUtil notify7 = new NotifyUtil(this, 7);
  notify7.notify_progress(rightPendIntent, smallIcon, ticker, "快圖浏覽升級程序", "正在下載中",
    false, false, false, download_url, savePath, new NotifyUtil.DownLoadListener() {
     @Override
     public void OnSuccess(File file) {
      mFile=file;
      DownLoadService.this.stopSelf();
     }

     @Override
     public void onFailure(Throwable t, int errorNo, String strMsg) {

     }
    });
  currentNotify = notify7;
  return super.onStartCommand(intent, flags, startId);

 }
}

在調用“notify_progress”方法的時候,已經開始下載文件了,那麼下載的代碼是什麼呢?如下:

public void notify_progress(PendingIntent pendingIntent, int smallIcon,
        String ticker, String title, String content,
        boolean sound, boolean vibrate, boolean lights,
        String download_url, String savePath, final DownLoadListener listener) {

  setCompatBuilder(pendingIntent, smallIcon, ticker, title, content, sound, vibrate, lights);
  /*
   * 因為進度條要實時更新通知欄也就說要不斷的發送新的提示,所以這裡不建議開啟通知聲音。
   * 這裡是作為范例,給大家講解下原理。所以發送通知後會聽到多次的通知聲音。
   */
  FinalHttp fh = new FinalHttp();
  HttpHandler<File> httpHandler=fh.download(download_url, savePath, new AjaxCallBack<File>() {
   @Override
   public void onLoading(long count, long current) {
    super.onLoading(count, current);
    double a=count;
    double b=current;
    double currentPro=(double)((b/a)*100);
    cBuilder.setProgress(100, (int)currentPro, false);
    sent();
   }

   @Override
   public void onSuccess(File file) {
    super.onSuccess(file);
    cBuilder.setContentText("下載完成").setProgress(0, 0, false);
    sent();
    listener.OnSuccess(file);
   }

   @Override
   public void onFailure(Throwable t, int errorNo, String strMsg) {
    super.onFailure(t, errorNo, strMsg);
    listener.onFailure(t,errorNo,strMsg);
   }

  });


 }

這裡用到了afinal.jar

這個jar已經封裝好下載的工具類,我們直接拿來用就行。下載成功之後會通過DownLoadListener這個接口回調到DownLoadService裡面,最終運行效果就如最上面那個gif動態圖運行效果一樣。

項目下載地址:點擊下載

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved