多线程java Android服务
我已经建立了一个列表视图。每个ListView项都包含一个ToggleButton, 它启动或停止服务中的线程
只要切换按钮被激活,线程就需要运行。如果活动停止,服务和激活的线程需要继续
//In MainActivity
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Intent intent = new Intent(context,MyService.class);
intent.putExtra("id",listviewPosition);
startService(intent);
}
//MyService
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Thread(startId) starten
HandlerThread thread = new HandlerThread(String.valueOf(startId));
thread.start();
// Threads looper holen und beim servicehandler benutzen
serviceHandler = new ServiceHandler(thread.getLooper());
listServericeHandler.add(serviceHandler);
//message ausm pool holen
Message msg = serviceHandler.obtainMessage();
msg.arg1 = startId;
serviceHandler.sendMessage(msg);
return START_STICKY;
}
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
while (continueUntilToggleButtonIsDeactivated) {
Log.d(MainActivity.TAG, "Thread " + tring.valueOf(msg.arg1)
+ " running: " + url.toExternalForm());
synchronized (this) {
try {
//do stuff
wait(1000);
} catch (Exception e) {
}
}
}
}
}
所以,每次我激活一个切换按钮时,我都试图在OnStartCommand()
中启动一个线程
我不知道的是如何停止线程并使其退出while循环
# 1 楼答案
您只需要为while循环创建一个要遵守的标志,就可以知道何时停止。例如:
编辑
您应该在服务的
onCreate()
方法中创建处理程序。onStartCommand
方法会通知您新的消息然后将ServiceHandler更改为