请问是什么原因导致的进度条更新乱序呢,怎么解决????public class DownloadService extends Service { NotificationManager mManager;
Notification mNotification;
String mDownloadDir = "/sdcard/FTP_download"; @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
} Handler mHandler = new Handler(new Handler.Callback() { @Override
public boolean handleMessage(Message msg) {
// TODO Auto-generated method stub
Bundle bundle = msg.getData();
int id = bundle.getInt(Constant.NOTIFICATION_ID, -999);
String max = bundle.getString(Constant.NOTIFICATION_MAX);
String down = bundle.getString(Constant.NOTIFICATION_DOWN);
int percent = bundle.getInt(Constant.NOTIFICATION_PERCENT);
String speed = bundle.getString(Constant.NOTIFICATION_SPEED);
if (id < 0) {
return false;
}
updateNotification(id, percent, speed);
return false;
}
}); void updateNotification(int id, int percent, String speed) {
Notification notification = new Notification(R.drawable.ic_launcher, "进度条", System.currentTimeMillis());
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_layout);
contentView.setProgressBar(R.id.notification_down_progress_bar, 100, percent, false);
contentView.setTextViewText(R.id.notification_down_progress_bar_speed, "id=" + id + ";" + speed + ";" + percent + "%");
notification.contentView = contentView;
Intent notificationIntent = new Intent(this, DownloadActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
mManager.notify(id, notification);
} void addNotification(int id) {
Notification notification = new Notification(R.drawable.dd, "开始下载了...", System.currentTimeMillis());
RemoteViews view = new RemoteViews(DownloadService.this.getPackageName(), R.layout.notification_layout);
view.setProgressBar(R.id.notification_down_progress_bar, 100, 0, false);
view.setTextViewText(R.id.notification_down_progress_bar_speed, "speed");
notification.contentView = view;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent mIntent = new Intent(DownloadService.this, DownloadActivity.class);
// mIntent.setAction(Constant.DOWNLOAD_ACTION);
mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
mIntent.putExtra("appId", 100);
PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, "正在下载", "下载详情,请点击查看", contentIntent);
mManager.notify(id, notification);
} @Override
public void onCreate() {
// TODO Auto-generated method stub
mManager = (NotificationManager) getSystemService(this.NOTIFICATION_SERVICE);
super.onCreate();
} @Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
printLog("-------onStart----开启下载服务---"); Bundle bundle = intent.getExtras();
List<String> tasks = bundle.getStringArrayList(Constant.TASK);
String ip = bundle.getString(Constant.SERVER_IP);
String password = bundle.getString(Constant.SERVER_PASSWORD);
String username = bundle.getString(Constant.SERVER_USERNAME);
String port = bundle.getString(Constant.SERVER_PORT);
String task = bundle.getString(Constant.TASK); Util util = new Util(mHandler, new Client(ip, username, password, port), this);
// for (String task : tasks) {
int id = MyApplication.getId();
util.down(task, mDownloadDir, getLoacalName(task), id);
// updateNotification(id, 0, "开始下载");
addNotification(id);
// }
printLog("--------------xxxxxx-----dddddd----------");
super.onStart(intent, startId);
} public String getLoacalName(String task) {
return task.substring(task.lastIndexOf("/") + 1);
} void printLog(String msg) {
Log.e("DownloadService", msg);
}}