为什么 BroadcastReceiver 在 Android 条件下不进入 else
Why BroadcastReceiver is not going in else if condition in Android
我正在尝试从服务向 Activity 发送动态广播。当我从 Notification.class 服务发送广播时,Activity 接收到该广播。但是当我尝试从 sendData.class 服务发送广播时,Activity 没有收到该数据。我不知道发生了什么事。
这是我的 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());
}
我试过很多次了。但它不起作用。
将 MUSIC_PLAYER
添加到下面 onCreate
中的意向过滤器
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(mBroadcastStringAction);
mIntentFilter.addAction(MUSIC_PLAYER);
我正在尝试从服务向 Activity 发送动态广播。当我从 Notification.class 服务发送广播时,Activity 接收到该广播。但是当我尝试从 sendData.class 服务发送广播时,Activity 没有收到该数据。我不知道发生了什么事。 这是我的 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());
}
我试过很多次了。但它不起作用。
将 MUSIC_PLAYER
添加到下面 onCreate
中的意向过滤器
mIntentFilter = new IntentFilter();
mIntentFilter.addAction(mBroadcastStringAction);
mIntentFilter.addAction(MUSIC_PLAYER);