Android实现Service下载⽂件,Notification显⽰下载进度
的⽰例
先放个gif。。最终效果如果:
主要演⽰了Android从服务器下载⽂件,调⽤Notification显⽰下载进度,并且在下载完毕以后点击通知会跳转到安装APK的界⾯,演⽰是在真实的⽹络环境中使⽤真实的URL进⾏演⽰,来看看代码:
MainActivity代码⾮常简单,就是启动⼀个Service:
public class MainActivity extends AppCompatActivity {
String download_url="shouji.360tpcdn/160329/a9037075b8d3aa98fbf6115c54a5b895/com.alensw.PicFolder_4722404.apk";
@Override
protected void onCreate(Bundle 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= ExternalStorageDirectory()+"/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() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mFile=new File(savePath);
download_StringExtra("download_url");
Log.e("test","执⾏onStartCommand");
ajax实例 文件浏览//设置想要展⽰的数据内容
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 = Activity(this,
requestCode, intent_noti, PendingIntent.FLAG_UPDATE_CURRENT);
int smallIcon = _smaillicon;
String ticker = "正在更新快图浏览";
//实例化⼯具类,并且调⽤接⼝
NotifyUtil notify7 = new NotifyUtil(this, 7);
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;
StartCommand(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) {
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) {
cBuilder.setContentText("下载完成").setProgress(0, 0, false);
sent();
listener.OnSuccess(file);
}
@Override
public void onFailure(Throwable t, int errorNo, String strMsg) {
}
});
}
这⾥⽤到了
这个jar已经封装好下载的⼯具类,我们直接拿来⽤就⾏。下载成功之后会通过DownLoadListener这个接⼝回调到DownLoadService⾥⾯,最终运⾏效果就如最上⾯那个gif动态图运⾏效果⼀样。
项⽬下载地址:
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。