广播接收器未收到

时间:2015-11-11 21:22:56

标签: java android broadcastreceiver

我知道这个问题经常出现在这里,但我已经查看了大概20个堆栈溢出问题并且无法找到解决方案。我很确定这很简单,我做错了,但我对Android很新,这项任务将在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 个答案:

答案 0 :(得分:1)

无论您是在Intents(用于系统级广播)还是在{{1 (对于本地广播)。

相反:

  • 定义操作字符串(例如registerReceiver()

  • 在创建registerReceiver()广播(Context)时使用该操作字符串

  • 在创建LocalBroadcastManagerfinal String ACTION="com.dellosa.nick.ITS_HUMP_DAY";

  • 时使用该操作字符串