准备用expandablelistview做 多线程下载 ,但现在出了一个问题 一直找不到问题所在, 我打印了几乎是所有的相关数据,成员ID,名字等等来对照均没有发现错误。
问题是这样的,当我同时运行两个下载线程的时候,我每启动下载的时候,都会NEW一个handlerthread(名字是递加的)存放到ARRAYLIST里面,这个ARRAYLIST是专门存放所有下载线程的,也就是所谓有多少个下载在进行就会存放多少个线程及HANDLER的这样我就可以在下面的BASEEXPANDABLELISTADPATER里面 来为每一个CHILD加入控件VIEW,下面这段就是我的返回CHILD的方法,我为每一条下载线程旁边,添加了一个BUTTON来控制暂停和继续的, 我的下载程序在同时下载2个以下,包括2个的时候,无论怎么暂停继续都没问题,下载很正常,但下载超过3个,包括3个,也就是同时下载3个以上的时候,会出现点击这个按钮,反而暂停了其它下载线程,继续了另一个,但这个还不动,必须同时开启两个才能正常跑起来下载,但如果是加入3个以上的时候,不要点击按钮暂停,那么下载是完全正常的,起初我有过怀疑我的继续下载方法有问题,但里面其实就是一个WHILE循环 加个BOOLEAN 来控制暂停继续的,通过存放HANDLERTHREAD的ARRAYLIST来启动相应CHILD对应的HANDLER,点继续的时候会发送到HANDLER信息,我的WHAT=0是新建下载 WHAT=1是继续下载 WHAT=2是暂停下载。可我觉得按这个逻辑应该不会出问题才对,可是还是出现了这个问题,实在找不到问题所在了,。
拜托各位大神,谁能指点一下迷津,非常感谢!!!
// 创建组/子视图
public View getGenericViewChild(String s, int groupid, final int childid) {
View view = View.inflate(context, R.layout.downloadchild, null);
TextView text = (TextView) view.findViewById(R.id.newappname);
text.setText(s);
text.setPadding(60, 0, 0, 0);
text.setTextSize(14);
TextView text2 = (TextView) view.findViewById(R.id.newappcount);
final Button button = (Button) view.findViewById(R.id.newappbutton);
// final Button button = C.downloadcontrolbutton.get(childid);
if (groupid == 0) {
if (C.downloadmthread.get(childid) != null) {
if (C.downloadmthread.get(childid).stop)
button.setText("继续");
else
button.setText("暂停");
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (button.getText().equals("暂停")) {
C.downloadmthread.get(childid).stop = true;
Message msg = C.downloadmthread.get(childid)
.obtainMessage();
msg.what = 2;
msg.sendToTarget();
button.setText("继续");
} else {
C.downloadmthread.get(childid).stop = false;
Message msg2 = C.downloadmthread.get(childid)
.obtainMessage();
msg2.what = 1;
msg2.sendToTarget();
button.setText("暂停");
}
}
});
text2.setText(C.downloadmthread.get(childid).nowpart + "% "
+ C.downloadmthread.get(childid).totalpart + "MB");
text2.setPadding(60, 0, 0, 0);
text2.setTextSize(12);
}
} else {
button.setText("运行");
}
return view;
}
下面这段是点击下载链接的时候触发的方法:public void openthreadtodownload(String name, final String url) {
HandlerThread thread = new HandlerThread("downloadfile" + downloadindex);
thread.setPriority(Thread.NORM_PRIORITY);
thread.start();
DownloadHandler myhandler = new DownloadHandler(thread.getLooper(),
url, name, "downloadfile" + downloadindex, C.downloadmthread
.size());
C.downloadmthread.add(myhandler);
Message msg = myhandler.obtainMessage();
msg.what = 0;
msg.sendToTarget();
}
下面是 继续下载调用的内容while (stop2 == false && (readLen = in.read(temp)) > 0) {
destPos += readLen;
nowpart = (float) (Math.round(destPos / length * 100 * 100)) / 100;
// nowpart = destPos;
fos.write(temp, 0, readLen);
if (stop) {
stop2 = true;
}
}这个WHILE我打印过,发现当我点击继续按钮的时候,会进入这个WHILE下载1秒左右 然后WHILE很奇怪的就跳出了,我打印过STOP2和STOP的值都是FALSE 是绝对不会满足条件跳出的,真不知道是怎么回事了?