[英]Broadcast Receiver not working after reboot android
我正在使用 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
}
}
}
}
}
我用這個代碼注冊它。
registerReceiver(myReceiver, IntentFilter(Intent.ACTION_HEADSET_PLUG))
沒關系,但是當設備重新啟動時不起作用。我已經檢查了一些相關問題,但沒有找到解決此問題的任何方法。 所以這對我來說是一個全新的問題。
您需要在清單中注冊廣播接收器:
<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.