BroadcastReceiver的onReceive方法没有被调用
BroadcastReceiver's onReceive method is not called
我有一个 Activity,里面有 Fragment 和 IntentService。当我单击按钮时,对话框片段打开并输入我的数据。之后我单击 Ok,它调用 getActivity().startService(intent)。服务通过 LocalBroadcastManager 调用 sendBroadcast(intent),但没有调用 onReceive() 方法。怎么了?这是我的 BroadcastReceiver
private BroadcastReceiver mApiBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("tag", intent.getAction());
switch (intent.getAction()) {
case "register":
showMessage(intent.getStringExtra(ApiService.EXTRA_ANSWER));
break;
case "authorize":
showProfile((User) intent.getParcelableExtra(ApiService.EXTRA_USER));
break;
default:
break;
}
}
};
我在这里注册接收者
@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.
getInstance(this).
registerReceiver(mApiBroadcastReceiver, new IntentFilter(ApiService.API_BROADCAST));
}
我的 onDestroy 方法
@Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mApiBroadcastReceiver);
}
以及我在服务中发送广播的方法
private void sendMessage(Object answer) {
Intent intent = new Intent(API_BROADCAST);
if (answer instanceof String) {
intent.setAction("register");
Log.d("tag", answer.toString());
intent.putExtra(EXTRA_ANSWER, answer.toString());
} else if (answer instanceof User) {
intent.setAction("authorize");
intent.putExtra(EXTRA_USER, (User) answer);
}
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
你不应该打电话给 intent.setAction()
。
您正在监听带有动作的 Intent ApiService.API_BROADCAST
:
- 注册时,您使用
new IntentFilter(ApiService.API_BROADCAST)
;
- 广播时使用
new Intent(ApiService.API_BROADCAST)
;
没关系,但如果您稍后调用 intent.setAction("register")
,您将覆盖您设置的第一个操作。
相反,尝试将子操作字符串作为 Extra
:
private void sendMessage(Object answer) {
Intent intent = new Intent(API_BROADCAST);
if (answer instanceof String) {
intent.putExtra(EXTRA_WHAT, "register");
intent.putExtra(EXTRA_ANSWER, answer.toString());
} else if (answer instanceof User) {
intent.putExtra(EXTRA_WHAT, "authorize");
intent.putExtra(EXTRA_USER, (User) answer);
}
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
我有一个 Activity,里面有 Fragment 和 IntentService。当我单击按钮时,对话框片段打开并输入我的数据。之后我单击 Ok,它调用 getActivity().startService(intent)。服务通过 LocalBroadcastManager 调用 sendBroadcast(intent),但没有调用 onReceive() 方法。怎么了?这是我的 BroadcastReceiver
private BroadcastReceiver mApiBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("tag", intent.getAction());
switch (intent.getAction()) {
case "register":
showMessage(intent.getStringExtra(ApiService.EXTRA_ANSWER));
break;
case "authorize":
showProfile((User) intent.getParcelableExtra(ApiService.EXTRA_USER));
break;
default:
break;
}
}
};
我在这里注册接收者
@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.
getInstance(this).
registerReceiver(mApiBroadcastReceiver, new IntentFilter(ApiService.API_BROADCAST));
}
我的 onDestroy 方法
@Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mApiBroadcastReceiver);
}
以及我在服务中发送广播的方法
private void sendMessage(Object answer) {
Intent intent = new Intent(API_BROADCAST);
if (answer instanceof String) {
intent.setAction("register");
Log.d("tag", answer.toString());
intent.putExtra(EXTRA_ANSWER, answer.toString());
} else if (answer instanceof User) {
intent.setAction("authorize");
intent.putExtra(EXTRA_USER, (User) answer);
}
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
你不应该打电话给 intent.setAction()
。
您正在监听带有动作的 Intent ApiService.API_BROADCAST
:
- 注册时,您使用
new IntentFilter(ApiService.API_BROADCAST)
; - 广播时使用
new Intent(ApiService.API_BROADCAST)
;
没关系,但如果您稍后调用 intent.setAction("register")
,您将覆盖您设置的第一个操作。
相反,尝试将子操作字符串作为 Extra
:
private void sendMessage(Object answer) {
Intent intent = new Intent(API_BROADCAST);
if (answer instanceof String) {
intent.putExtra(EXTRA_WHAT, "register");
intent.putExtra(EXTRA_ANSWER, answer.toString());
} else if (answer instanceof User) {
intent.putExtra(EXTRA_WHAT, "authorize");
intent.putExtra(EXTRA_USER, (User) answer);
}
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}