Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在线程内注册BroadcastReceiver()_Android_Broadcastreceiver - Fatal编程技术网

Android 如何在线程内注册BroadcastReceiver()

Android 如何在线程内注册BroadcastReceiver(),android,broadcastreceiver,Android,Broadcastreceiver,我有一个录音机android线程,我需要知道录音时麦克风/耳机是否连接,所以我需要在线程中使用BroadcastReceiver()。我如何注册?此.registerReceiver()不起作用,因为它只在活动内部起作用 如果在线程内使用broadcasereceivers不是一个好主意,那么解决方案是什么 下面是在活动中工作而在线程中不工作的代码: headsetReceiver = new BroadcastReceiver() { @Override

我有一个录音机android线程,我需要知道录音时麦克风/耳机是否连接,所以我需要在线程中使用BroadcastReceiver()。我如何注册?此.registerReceiver()不起作用,因为它只在活动内部起作用

如果在线程内使用broadcasereceivers不是一个好主意,那么解决方案是什么

下面是在活动中工作而在线程中不工作的代码:

    headsetReceiver = new BroadcastReceiver() {
        @Override
            public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.i("Broadcast Receiver", action);
            if ((action.compareTo(Intent.ACTION_HEADSET_PLUG)) == 0) // if
                                                                        // the
                                                                        // action
                                                                        // match
                                                                        // a
                                                                        // headset
                                                                        // one
            {
                int headSetState = intent.getIntExtra("state", 0); // get
                                                                    // the
                                                                    // headset
                                                                    // state
                                                                    // property
                int hasMicrophone = intent.getIntExtra("microphone", 0);// get
                                                                        // the
                                                                        // headset
                                                                        // microphone
                                                                        // property
                if ((headSetState == 0) && (hasMicrophone == 0)) // headset
                                                                    // was
                                                                    // unplugged
                                                                    // &
                                                                    // has
                                                                    // no
                                                                    // microphone
                {
                    // do whatever
                }
            }
        }
    };

    this.registerReceiver(headsetReceiver, new IntentFilter(
            Intent.ACTION_HEADSET_PLUG));

您需要将上下文传递给线程构造函数,然后使用它注册广播接收器:

//this.ctx is passed to the Thread constructor
this.ctx.registerReceiver(headsetReceiver, new IntentFilter(
            Intent.ACTION_HEADSET_PLUG));
不要忘记在线程中的finally{}中注销接收器,否则可能会发生泄漏:

finally{
       ctx.unregisterReceiver(headsetReceiver);
}

要更改主线程(如活动)内的UI,您需要设置处理程序。您需要将上下文传递给线程构造函数,然后使用它注册广播接收器:

//this.ctx is passed to the Thread constructor
this.ctx.registerReceiver(headsetReceiver, new IntentFilter(
            Intent.ACTION_HEADSET_PLUG));
不要忘记在线程中的finally{}中注销接收器,否则可能会发生泄漏:

finally{
       ctx.unregisterReceiver(headsetReceiver);
}
为了在主线程(如活动)内更改UI,您需要设置一个处理程序