Android實現Service下載檔案,Notification顯示下載進度
阿新 • • 發佈:2019-01-27
先放個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動態圖執行效果一樣。
專案下載地址:點選下載