public class CallEvent extends BroadcastReceiver{
public LEDController ledController = new LEDController();
public ApplicationSettings applicationSettings = new ApplicationSettings();
public boolean ring = false;
@Override
public void onReceive(Context context, Intent intent){
if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)){
ring = true;
blink();
}else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE) ||
intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
ring = false;
}
}
public void blink(){
Runnable r = new Runnable() {
@Override
public void run() {
while(ring){
ledController.turnOnFlash();
try {
Thread.sleep(applicationSettings.getDelayOn());
} catch (InterruptedException e) {
e.printStackTrace();
}
ledController.turnOffFlash();
try {
Thread.sleep(applicationSettings.getDelayOff());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread blinkThread = new Thread(r);
blinkThread.start();
}
}
我想在手机响铃时创建led信使。但是我无法停止blinkThread。我不知道它不起作用。在通话收入时启动,但在通话减少时不要停止。可变环正在改变“假”。当呼叫减少,但线程仍在工作
答案 0 :(得分:0)
一般情况下,您不会强行停止线程,因为它很危险。你设置了一个标志,告诉有问题的线程在受控环境下退出它的线程循环。
你的线程循环看起来像这样:
void run() {
while (shouldContinue) {
doThreadWorkUnit();
}
}
在其他地方设置shouldContinue
变量并等待线程完成:
...
thread.shouldContinue = false;
thread.join();
...
(所有这些可能都不正确Java,因为我不做Java。将其视为伪代码并修改您的实际语言/线程库/等。)