不调用广播接收器onReceive() [英] Broadcast receiver onReceive() is not called
本文介绍了不调用广播接收器onReceive()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
广播接收器
mBroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Log.i(TAG,"contact list populate broadcast")
var action = intent.action
Log.v("Action is ",action)
when (action) {
GetContactListFromServer -> {
println("Get contact list")
}
}
}
}
val filter = IntentFilter(GetContactListFromServer)
this.registerReceiver(mBroadcastReceiver, filter)
这就是我发送广播的方式
This is how I am sending broadcast
val i = Intent(ContactListActivity.Obz.GetContactListFromServer)
i.`package` = mApplicationContext?.getPackageName()
mApplicationContext?.sendBroadcast(i)
此代码可以正常工作到昨晚.我无法调试出了什么问题?
This code is working fine till last night. I am not able to debug what is going wrong?
推荐答案
像这样尝试
在您的活动中,在 onCreate 方法中声明
In your activity Inside onCreate method declare
LocalBroadcastManager.getInstance(context).registerReceiver(MyBroadcastReceiver, new IntentFilter("MyFilter"));
然后在Activity中实现MyBroadcastReceiver
Then implement the MyBroadcastReceiver in Activity
private final BroadcastReceiver MyBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String Action = intent.getStringExtra("Action");
switch (Action){
case "updatedata":
String message = intent.getStringExtra("message");
String date_time = intent.getStringExtra("date_time");
break;
default:
break;
}
}
};
像这样发送广播
Intent intent = new Intent("MyFilter");
intent.putExtra("message", message);
intent.putExtra("date_time", date_time);
intent.putExtra("Action", "updatedata");
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
这篇关于不调用广播接收器onReceive()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文