Android實現(xiàn)Service下載文件,Notification顯示下載進(jìn)度的示例
先放個gif。。最終效果如果:

主要演示了Android從服務(wù)器下載文件,調(diào)用Notification顯示下載進(jìn)度,并且在下載完畢以后點擊通知會跳轉(zhuǎn)到安裝APK的界面,演示是在真實的網(wǎng)絡(luò)環(huán)境中使用真實的URL進(jìn)行演示,來看看代碼:
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方法里面是關(guān)鍵代碼,調(diào)用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","執(zhí)行onStartCommand");
//設(shè)置想要展示的數(shù)據(jù)內(nèi)容
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 = "正在更新快圖瀏覽";
//實例化工具類,并且調(diào)用接口
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);
}
}
在調(diào)用“notify_progress”方法的時候,已經(jīng)開始下載文件了,那么下載的代碼是什么呢?如下:
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);
/*
* 因為進(jìn)度條要實時更新通知欄也就說要不斷的發(fā)送新的提示,所以這里不建議開啟通知聲音。
* 這里是作為范例,給大家講解下原理。所以發(fā)送通知后會聽到多次的通知聲音。
*/
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已經(jīng)封裝好下載的工具類,我們直接拿來用就行。下載成功之后會通過DownLoadListener這個接口回調(diào)到DownLoadService里面,最終運(yùn)行效果就如最上面那個gif動態(tài)圖運(yùn)行效果一樣。
項目下載地址:點擊下載
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 中為什么要用Fragment.setArguments(Bundle bundle)來傳遞參數(shù)
這篇文章主要介紹了Android 中為什么要用Fragment.setArguments(Bundle bundle)來傳遞參數(shù),非常不錯,具有參考借鑒價值,需要的朋友參考下2017-01-01
Android內(nèi)容提供者ContentProvider用法實例分析
這篇文章主要介紹了Android內(nèi)容提供者ContentProvider用法,結(jié)合實例形式較為詳細(xì)的分析了內(nèi)容提供者ContentProvider獲取及解析數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2016-03-03
Android中NavigationView的使用與相關(guān)問題解決
大家都知道NavigationView的引入讓 Android側(cè)邊欄實現(xiàn)起來相當(dāng)方便,最近公司項目中也使用這個新的控件完成了側(cè)邊欄的改版。在使用過程中遇到一些問題所以記錄一下。本文分為兩個部分,一是基本使用,二是相關(guān)問題的解決,感興趣的朋友們下面來一起看看吧。2016-10-10
Android 源碼淺析RecyclerView ItemAnimator
這篇文章主要為大家介紹了Android 源碼淺析RecyclerView ItemAnimator,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android進(jìn)階手寫IPC通信框架告別繁瑣AIDL
這篇文章主要為大家介紹了Android進(jìn)階手寫IPC通信框架告別繁瑣AIDL實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android開發(fā)Input系統(tǒng)觸摸事件分發(fā)
這篇文章主要為大家介紹了Android開發(fā)Input系統(tǒng)觸摸事件分發(fā)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android自定義雙向進(jìn)度條的實現(xiàn)代碼
本篇文章主要介紹了Android自定義雙向進(jìn)度條的實現(xiàn)代碼,非常具有實用的價值,有興趣的同學(xué)一起來了解一下2017-09-09

