只需要一串 URL 地址,就可以实现 后台多任务下载的demo,notification进度条更新信息封装好直接提取,更新频率等问题都已优化。
demo源码下载地址在CSDN上和eoe android里都有,这边就给个链接吧。
由于是很久之前的在eoe android 社区里发帖讨论回复过相关问题了,这里就发个原帖地址链接吧。
service后台多任务下载notification进度条断点续传。
首先是service服务文件,内部包含相关注解。主要是支持后台下载,并且更新下载进度到状态栏。
package com.ljp.download.service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;
import com.ljp.download.tempActivity;
import com.ljp.version.R;
public class DownloadService extends Service {
private NotificationManager notificationMrg;
public static Map<String, MyThread> threadcache = new HashMap<String, MyThread>();
private Holder holder;
int notifyflag = 1;
private ConnectivityManager connectivityManager;
private NetworkInfo info;
public void onCreate() {
super.onCreate();
notificationMrg = (NotificationManager) this
.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
// 注册网络监听接收器
IntentFilter mFilter = new IntentFilter();
mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
mFilter.addAction("android.basic.notification.click.pause");
mFilter.addAction("android.basic.notification.click.cancel");
mFilter.addAction("android.basic.notification.click.restart");
registerReceiver(stateReceiver, mFilter);
// 注册任务广播接收器
}
@SuppressWarnings("null")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
System.out.println("Get intent:" + intent);
final String apkurl = intent.getStringExtra("url");
System.out.println("Get url from intent:" + apkurl);
Intent notificationIntent = new Intent(getApplicationContext(),
tempActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("url", apkurl);
Bundle bundle = new Bundle();
bundle.putInt("flag", notifyflag);
Log.i("avi", "loadfile中的flag " + notifyflag);
notificationIntent.putExtras(bundle);
// addflag设置跳转类型
PendingIntent contentIntent1 = PendingIntent.getActivity(
getApplicationContext(), notifyflag, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// 创建Notifcation对象,设置图标,提示文字
Notification notification = new Notification(R.drawable.ic_launcher,
"下载进度", System.currentTimeMillis());// 设定Notification出现时的声音,一般不建议自定义
// System.currentTimeMillis()
notification.flags = Notification.FLAG_ONGOING_EVENT
| Notification.FLAG_NO_CLEAR;// 出现在 “正在运行的”栏目下面
// notification.flags |= Notification.FLAG_NO_CLEAR;// 标题栏默认清除按钮失效
RemoteViews contentView1 = new RemoteViews(getPackageName(),
R.layout.notification_version);
contentView1.setTextViewText(R.id.n_title, "准备下载");
contentView1.setTextViewText(R.id.n_t