我试图将动态广播从服务发送到活动。当我从Notification.class服务发送广播时,活动接收广播。但是当我尝试从sendData.class Service发送广播时,Activity没有收到该数据。我不知道发生了什么。 这是我的活动代码: -
protected void onCreate(Bundle savedInstanceState) {
.
.
.
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(mBroadcastStringAction);
.
.
.
.
.
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onReceive(Context context, Intent intent) {
// This Broadcast is called by getData.class service and not working :(
if(intent.getAction().equals(PLAY_MUSIC)){
Toast.makeText(context, "Broadcast Recieved", Toast.LENGTH_SHORT).show(); //Not showing Toast message also
String action = intent.getStringExtra("play");
if(action.equals("true")) {
playBtnClick();
}
}
//This Broadcast is Called by Notification.class & Working Properly
if (intent.getAction().equals(mBroadcastStringAction)) {
int position = intent.getIntExtra("position", 0);
int duration = intent.getIntExtra("duration", 0);
sb.setMax(duration);
sb.setProgress(position);
setSongDetails();
}
} //I have also try else if condition also
};
@Override
protected void onResume() {
super.onResume();
registerReceiver(mReceiver, mIntentFilter);
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
这是我发送广播的Notification.class
Intent broadcastIntent = new Intent();
broadcastIntent.setAction(MainPlayer.mBroadcastStringAction);
broadcastIntent.putExtra("duration", mediaPlayer.getDuration());
broadcastIntent.putExtra("position", mediaPlayer.getCurrentPosition());
sendBroadcast(broadcastIntent);
这是我的getData.class
try {
Intent intent = new Intent(MainPlayer.PLAY_MUSIC);
intent.putExtra("play","true");
sendBroadcast(intent);
Log.d("EasysPlay","BroadCast Send");
}catch(Exception e){
Log.d("EasysPlay",e.toString());
}
我已经尝试了很多次。但它没有用。
答案 0 :(得分:1)
将MUSIC_PLAYER
添加到onCreate
下方
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(mBroadcastStringAction);
mIntentFilter.addAction(MUSIC_PLAYER);