广播接收器不会停止一个线程 [英] BroadcastReceiver doesn't stop a thread

查看:109
本文介绍了广播接收器不会停止一个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做一个BrodcastReceiver说话消息:接收呼叫,而电话铃声。

I do an BrodcastReceiver to speak the message: Receiving Call, while phone ringing.

广播接收器的code为:

Code of BroadcastReceiver to:

    @Override
public void onReceive(Context context, Intent intent) {

    cont = context;
    tts = new Speak(cont);

    String action = intent.getAction();
    if (action.equalsIgnoreCase("android.intent.action.PHONE_STATE")) {
        if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                TelephonyManager.EXTRA_STATE_RINGING)) {

            incoming_number = intent.getExtras().getString(
                    "incoming_number");

            falador = new Thread() {
                @Override
                public void run() {
                    try {
                        if (!this.isInterrupted()) {
                            while (true) {
                                sleep(5000);
                                h.post(repeater);
                            }
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            falador.start();

        } else {
            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                    TelephonyManager.EXTRA_STATE_IDLE)) {

                h.removeCallbacks(repeater);
                falador.interrupt();
            }
        }
    }
}

不过,该事件IDLE不会停止线程。你能帮助我吗?

But, the event IDLE doesn't stop the thread. Can you help me?

谢谢,
马特乌斯

Thanks, Mateus

推荐答案

这个问题是旧的,但需要一个答案。 广播接收器 S上的应用程序的主线程和的onReceive 方法上来看有一个很短的续航时间。你可以认为,Android系统的建立和运行,并销毁该型接收器在后台的一个对象:

This question is old but needs an answer. BroadcastReceivers 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();

在此方法完成,相关对象(接收器)变得容易受到垃圾收集。这意味着什么创建/开始/连接到这个接收器可以立即被系统杀死。所以,你不应该(实际上切不可)做在的onReceive 方法的任何异步操作。 <一href=\"http://developer.android.com/reference/android/content/BroadcastReceiver.html#ReceiverLifecycle\"相对=nofollow>接收机生命周期和<一个href=\"http://developer.android.com/reference/android/content/BroadcastReceiver.html#onReceive%28android.content.Context,%20android.content.Intent%29\"相对=nofollow>的onReceive(上下文,意图)强调对这种情况。

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.

这篇关于广播接收器不会停止一个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆