BroadcastReceiver Doesn't Stop A Thread
I do an BrodcastReceiver to speak the message: Receiving Call, while phone ringing. Code of BroadcastReceiver to: @Override public void onReceive(Context context, Intent inten
Solution 1:
You could use:
falador = new Thread() {
@Override
public void run() {
try {
while (!Thread.interrupted()) {
sleep(5000);
h.post(repeater);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Then when you interrupt it will stop the thread
Solution 2:
This question is old but needs an answer. BroadcastReceiver
s run on the main thread of the application process and onReceive
method has a very short life time. You can think that Android system creates and runs and destroys an object of type of this receiver in the background:
BroadcastReceiver receiver = new YourReceiver();
receiver.onReceive(context, intent);
System.gc();
After this method finishes, related object (receiver) becomes susceptible to garbage collection. That means anything created/started/connected to this receiver can be killed immediately by the system. So you shouldn't (actually must not) do any asynchronous operation in the onReceive
method. Receiver Lifecyle and onReceive(context, intent) emphasize about this situation.
Post a Comment for "BroadcastReceiver Doesn't Stop A Thread"