1
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();
}

}

I want to create led messenger when phone is ring. But I can't stop blinkThread. I dont know it is not working. Led start when call income but don't stop when call decline. Variable ring is changing on 'false' when call decline, but thread still working

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Denis Makovsky
  • 460
  • 7
  • 22
  • You probably do not want to use a seperate thread for this, as it slows down the phone and consumes additional battery power. Instead, you could look at this answer (http://stackoverflow.com/a/10207775/1659409) to see how you could do it. When you no longer want to blink, you simply set a flag not to post the task again. – Bert Peters Jul 24 '15 at 15:02
  • use service for this ... broadcastreiver should not had own thread ... but you could start service ... then you can send intent to stop blink – Selvin Jul 24 '15 at 15:04
  • do you use blinkThread.interrupt(); ? – Paul Jul 24 '15 at 15:04
  • @Paul from where? are you assuming that it will be the same instance of the CallEvent reciver every time ? – Selvin Jul 24 '15 at 15:05
  • 2
    have you tried `public volatile boolean ring = false;` ? – Gennadii Saprykin Jul 24 '15 at 15:06
  • @GennadiiSaprykin beat me to volatile.... damn youz! DenisMakovsky you may want to consider writing a class that extends runnable that has some start and stop methods you can call into directly. – Mark W Jul 24 '15 at 15:07
  • it will not change anything ... as it will be (AFAIK) new instance of CallEvent every time ... static may help ... but still better way is to use service ... – Selvin Jul 24 '15 at 15:08
  • possible duplicate of [How to stop a thread?](http://stackoverflow.com/questions/4756862/how-to-stop-a-thread) – StephenG Jul 24 '15 at 15:34
  • no, it is not a duplicate ... he already implement this .... also volatile will not help ... as you can see http://ideone.com/XcVk74 it works without volatile ... – Selvin Jul 24 '15 at 16:24

1 Answers1

0

In general, you don't forcibly stop threads because it's dangerous. You set a flag that tells the thread in question to exit from it's thread loop under controlled circumstances.

Your thread loop looks something along these lines:

void run() {
  while (shouldContinue) {
    doThreadWorkUnit();
  }
}

And somewhere else you set the shouldContinue variable and wait for the thread to finish:

...
thread.shouldContinue = false;
thread.join();
...

(All this is likely not correct Java, since I don't do Java. View it as pseudo code and modify for your actual language/thread library/etc.)

Source: How to stop a thread?

Community
  • 1
  • 1
StephenG
  • 2,851
  • 1
  • 16
  • 36
  • 1
    Did you not see the ring flag in the OP? – Mark W Jul 24 '15 at 15:36
  • @MarkW no, he didn't ... he is a "googler"(kind of rep's farmer): he is finding the answer via google and paste 'em ("as is", without any added value, but at least he is giving a link to the source) ... sometimes keywords are not enough ... and answer is good, **but not for given question** ... – Selvin Jul 24 '15 at 16:31