gpt4 book ai didi

java - Android 多线程服务

转载 作者:太空宇宙 更新时间:2023-11-04 15:20:51 25 4
gpt4 key购买 nike

我已经设置了一个ListView。每个 ListView-Items 都包含一个 ToggleButton,它启动或停止服务中的线程。

只要切换按钮被激活,线程就需要运行。如果 Activity 停止,服务和激活的线程需要继续。

//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 循环。

最佳答案

您只需要为 while 循环创建一个标志来遵守即可知道何时停止。例如:

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) {}
}
}
}

编辑

您应该在服务的 onCreate() 方法中创建处理程序。 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();
}
}

然后将 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();
}
}

关于java - Android 多线程服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20405677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com