I know there are a bunch of questions related to this topic but nothing seems to work for me.
I have a service which receives broadcasts from multiple activities and I am differentiating them using different actions for each broadcast received.
Here is my code
MyService.java // This is service class
private BroadcastReceiver SleepTimeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG,"SleepTimeReceiver onReceive is called");
if (intent.getAction().equals("sleepTimeFinished")) {
Log.d(TAG,"intents are matching");
}
}
};
@Override
public void onCreate() {
Log.d(TAG,"onCreate is called");
.
.
registerReceiver(SleepTimeReceiver,new IntentFilter("sleepTimeFinished"));
}
@Override
public void onDestroy() {
Log.d(TAG, "on destroy is called");
unregisterReceiver(SleepTimeReceiver);
}
MyActivity.java // This is activity class
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG,"About to send broadcast");
Intent sleepTimeFinishedIntent = new Intent(this, MyService.class);
sleepTimeFinishedIntent.setAction("sleepTimeFinished");
sendBroadcast(sleepTimeFinishedIntent);
Log.d(TAG,"broadcast has been sent");
}
I have also tried to add the receiver in the manifest file but to no avail. In the logcat, I can see the logs from all methods except for the onReceive method of the receiver. Can someone help me on this. This simple issue has been bugging me for hours now.
Aucun commentaire:
Enregistrer un commentaire