gpt4 book ai didi

android - 为什么 BroadcastReceiver 在 Android 条件下不进入 else

转载 作者:行者123 更新时间:2023-11-30 00:25:04 25 4
gpt4 key购买 nike

我正在尝试从服务向 Activity 发送动态广播。当我从 Notification.class Service 发送 Broadcast 时,Activity 会收到该 Broadcast。但是当我尝试从 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 中的intent 过滤器

mIntentFilter = new IntentFilter();
mIntentFilter.addAction(mBroadcastStringAction);
mIntentFilter.addAction(MUSIC_PLAYER);

关于android - 为什么 BroadcastReceiver 在 Android 条件下不进入 else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45549040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com