请问是什么原因导致的进度条更新乱序呢,怎么解决????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);
}}
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);
}}
解决方案 »
- 读取android raw文件夹中数据库文件出现空指针异常
- LinearLayout线性布局, 添加Touch事件, 不成功????????????????????????
- ubuntu中 android源代码的使用求教
- 菜鸟求大神
- android
- 怎么把音乐文件放入工程啊,我要做播放器的!!
- android播放器怎么调用硬解
- android 布局及图片过多造成outofmemory问题
- android 传参的一个小问题
- listview接受接口数据,点击每个item跳转到对应Activity
- Fragment中获取网络数据的问题
- 可以android端向WebService端发送一个对象吗?八百里加急,求罩!!!!
我的知道是什么原因引起的了,是因为新的通知,都是new出来的,将通知保存在map中,下次更新的时候,直接从map中拿出来更新进度就ok了,可以以id为键,notification为值