I know this question comes here fairly often, but I've looked through probably 20 stack overflow questions already and haven't been able to find a solution. I'm fairly certain it's something simple I'm doing wrong but I'm pretty new to Android and this assignment is due in 7 hours or so.
Everything works up until the receiver being called. Here's the call, from a service
Intent intent = new Intent(getApplicationContext(), MainActivity.WatchReceiver.class);
intent.putStringArrayListExtra(CHANGEKEY, changedURLs);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(intent);
Now here's the receiver, nested inside the main activity
public class WatchReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(null, "broadcast received");
markAsChanged(intent.getStringArrayListExtra(WatchService.CHANGEKEY));
}
}
And the main activity's on start function, where I register the receiver
@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);
}
Explicit Intents
do not work with registerReceiver()
, whether you are calling registerReceiver()
on a Context
(for system-level broadcasts) or on an instance of LocalBroadcastManager
(for local broadcasts).
Instead:
Define an action string (e.g., final String ACTION="com.dellosa.nick.ITS_HUMP_DAY";
)
Use that action string when creating the Intent
to broadcast (new Intent(ACTION)
)
Use that action string when creating the IntentFilter
(new IntentFilter(ACTION)
)