java广播接收器未接收
我知道这个问题经常出现在这里,但我已经研究了大概20个堆栈溢出问题,还没有找到解决方案。我很确定这很简单,我做错了,但我对安卓相当陌生,这个作业大约7小时后就要交了
在呼叫接收器之前,一切正常。这是电话,从一个服务台打来的
Intent intent = new Intent(getApplicationContext(), MainActivity.WatchReceiver.class);
intent.putStringArrayListExtra(CHANGEKEY, changedURLs);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
现在是接收器,嵌套在主活动中
public class WatchReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(null, "broadcast received");
markAsChanged(intent.getStringArrayListExtra(WatchService.CHANGEKEY));
}
}
主要的活动是启动功能,在这里我注册接收者
@Override
protected void onStart() {
super.onStart();
// Bind to LocalService
wr = new WatchReceiver();
markedAsChanged = new ArrayList<Integer>();
LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(wr, new IntentFilter());
Intent intent = new Intent(this, WatchService.class);
sc = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
wb = (WatchService.WatchBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
wb = null;
}
};
bindService(intent, sc, Context.BIND_AUTO_CREATE);
}
# 1 楼答案
显式
Intents
不适用于registerReceiver()
,无论您是在Context
上调用registerReceiver()
(对于系统级广播)还是在LocalBroadcastManager
的实例上调用registerReceiver()
(对于本地广播)相反:
定义操作字符串(例如,
final String ACTION="com.dellosa.nick.ITS_HUMP_DAY";
)创建要广播的
Intent
(new Intent(ACTION)
)在创建
IntentFilter
(new IntentFilter(ACTION)
)