简体   繁体   English

重新启动android后广播接收器不起作用

[英]Broadcast Receiver not working after reboot android

I am using brad cast reciver for detect headset is conncet or not.this my BroadcastReceiver class.我正在使用 brad cast reciver 来检测耳机是否连接。这是我的 BroadcastReceiver 类。

class HeadsetIntentReceiver: BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent) {
            if (intent.action == Intent.ACTION_HEADSET_PLUG) {
                val state = intent.getIntExtra(STATE, NEGATIVE_ONE)
                    when (state) {
                        ZERO-> {//Headset  not is plugged
                          
                        }
                         ONE -> {//Headset is plugged
                           
                         }
                     }
            }
        }
    }

I register it with this code.我用这个代码注册它。

registerReceiver(myReceiver, IntentFilter(Intent.ACTION_HEADSET_PLUG))

that's ok but when device is reboot don't work.I have already checked some the related questions and have not found any solution for this problem.没关系,但是当设备重新启动时不起作用。我已经检查了一些相关问题,但没有找到解决此问题的任何方法。 So this is an absolutely new problem for me.所以这对我来说是一个全新的问题。

You need to register the broadcast receiver in the manifest:您需要在清单中注册广播接收器:

 <receiver android:name="yourPath.HeadsetIntentReceiver">
            <intent-filter>
                <action android:name="android.intent.action.ACTION_HEADSET_PLUG" />
            </intent-filter>
 </receiver>

像这样注册您的广播接收器

registerReceiver(HeadsetIntentReceiver, IntentFilter(Intent.ACTION_HEADSET_PLUG))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM