[英]Android Service with multiple Threads
I've set up a ListView. 我已经建立了一个ListView。 Each of the ListView-Items contains a ToggleButton, which starts or stops a Thread in a Service.
每个ListView-Items都包含一个ToggleButton,该按钮可启动或停止服务中的线程。
The threads need to run as long as the toggleButtons are activated. 只要激活toggleButtons,线程就需要运行。 If the activity stops, the service and the activated threads need to continue.
如果活动停止,则该服务和已激活的线程需要继续。
//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) {
}
}
}
}
}
So, I'm trying to start a thread in the OnStartCommand()
every time I activate a toggleButton. 因此,我每次激活toggleButton时都试图在
OnStartCommand()
启动线程。
What I don't know is how to stop the threads and get the out of the while-loop. 我不知道如何停止线程并退出while循环。
You just need to create a flag for the while loop to abide by to know when to stop. 您只需要为while循环创建一个标志即可遵守,以知道何时停止。 For example:
例如:
private final class ServiceHandler extends Handler {
private volatile boolean _continueRunning = true;
public ServiceHandler(Looper looper) {
super(looper);
}
public void stopRunning() {
_continueRunning = false;
this.notify(); //Interrupts the wait
}
@Override
public void handleMessage(Message msg) {
while (_continueRunning) { //Watches the variable
Log.d(MainActivity.TAG, "Thread " + tring.valueOf(msg.arg1)
+ " running: " + url.toExternalForm());
synchronized (this) {
try {
//do stuff
wait(1000);
} catch (Exception e) {}
}
}
}
EDIT 编辑
You should create your handler in the onCreate()
method of the service. 您应该在服务的
onCreate()
方法中创建处理程序。 The onStartCommand
method informs you of new messages. onStartCommand
方法通知您新消息。
//In your Service Class
...
private ServiceHandler _serviceHandler;
@Override
public void onCreate() {
HandlerThread thread = new HandlerThread("ServiceClassName");
thread.start();
// Threads looper holen und beim servicehandler benutzen
serviceHandler = new ServiceHandler(thread.getLooper());
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//message ausm pool holen
Message msg = _serviceHandler.obtainMessage();
msg.arg1 = startId;
serviceHandler.sendMessage(msg);
return START_STICKY;
}
private class mRunnable implements Runnable {
private volatile boolean _continueRunning = true;
@Override
public void run() {
while (_continueRunning) { //Watches the variable
synchronized (this) {
try {
//do stuff
wait(1000);
} catch (Exception e) {
//Purposefully left blank
}
}
}
}
public void stopRunning() {
_continueRunning = false;
this.notify();
}
}
Then you change your ServiceHandler to 然后您将ServiceHandler更改为
private final class ServiceHandler extends Handler {
//TODO whatever type of list you need
private List<mRunnable> _runningThreads = new LinkedList<mRunnable>();
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
mRunnable runnable = new mRunnable();
new Thread(runnable).start();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.