Hi all and thank you for your help!
I’ll start with the code
So, the class with the broadcast receiver is this:
public class MyService extends Service {
// ...
// ACTION
public static final String action = "com.mywebsite.myapp.package.class.action";
// ...
public void onCreate() {
// SET BROADCAST RECEIVER
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.w("broadcast receiver", "action: " + action);
if (action.equals("**")) {
Log.w("broadcast receiver", "**");
}
}
};
// REGISTER BROADCAST
final IntentFilter myFilter = new IntentFilter();
myFilter.addAction(action);
registerReceiver(this.broadcastReceiver, myFilter);
}
// ....
}
And I try to send a broadcast in this way from a fragment
Intent myIntent = new Intent(getActivity()
.getApplicationContext(), MediaPlayerService.class);
getActivity().startService(myIntent);
myIntent = new Intent(getActivity().getApplicationContext(),
MediaPlayerService.class);
myIntent.setAction(MyService.action);
myIntent.putExtra("data", "*******");
getActivity().sendBroadcast(myIntent);
However the broadcast receiver is never called. I can say this because of the logcat: the line Log.w(“broadcast receiver”, “action: ” + action); is never called. How can I resolve?
Thank you!
EDIT: class code:
public class MediaPlayerService extends Service {
private MediaPlayer mediaPlayer = null;
private AudioManager audioManager;
private BroadcastReceiver broadcastReceiver;
private String absoluteFilePath;
private Boolean areThereAnyErrors = false;
private int savedVolume;
// ACTIONS
public static final String prepareAndPlayNewFile = "com.disgustingapps.player.AudioManagement.MediaPlayerService.prepareAndPlayNewFile";
@Override
public void onCreate() {
Log.w("MediaPlayerService", "onCreate called");
// SET BROADCAST RECEIVER
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.w("broadcast receiver", "action: " + action);
if (action.equals("prepareAndPlayNewFile")) {
Log.w("broadcast receiver", "prepareAndPlayNewFile");
prepareAndPlayNewFile(intent
.getStringExtra("absoluteFilePath"));
}
}
};
// REGISTER BROADCAST
final IntentFilter myFilter = new IntentFilter();
myFilter.addAction(prepareAndPlayNewFile);
registerReceiver(this.broadcastReceiver, myFilter);
}
@Override
public void onDestroy() {
Log.w("MediaPlayerService", "onDestroy called");
unregisterReceiver(broadcastReceiver);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.w("MediaPlayerService", "onStartCommand called");
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
public void prepareAndPlayNewFile(String absoluteFilePath) {
// ...do something...
}
}
The reason you are not receiving the
Intentis because the receiver is inonCreate().context.startService()will only pass theActivity‘sIntentto theService‘sonStartCommand(). TheServicehas already been created by then (or should have), so trying to receive inonCreate()will never work.More info here
Trying to receive in
onStartCommand()will only work once (guess). But having aBroadcastReceiverinonStartCommandwill only get you the data if you callstartService(). Since you are calling this anyway, it’s far easier just do ditch theBroadcastReceiverand just pass the data with theIntentdelivered to theServicesonStartCommand().EDIT
Then just do this:
startService():