Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 實戰:用 Service 下載文件並在通知欄實時顯示進度

編輯:關於 Android 程式設計

先看效果圖,直觀感受一下最終成果:

這個示例完整演示了:從伺服器下載文件、借助 Notification 在通知欄展示下載進度,並在下載完成後點擊通知直接跳轉到 APK 安裝界面。整個演示都在真實網路環境下、用真實 URL 進行,來看看代碼。

MainActivity 非常簡單,職責就是啟動一個 Service:

 public class MainActivity extends AppCompatActivity { String download_url="https://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,它已經把下載邏輯封裝成了現成的工具類,直接拿来用即可。下載成功後會通過 DownLoadListener 接口回調到 DownLoadService 中,最終的運行效果就是最上面那張 gif 所展示的樣子。

項目源碼可點擊下載獲取。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved