gpt4 book ai didi

Android BroadcastReceiver 永远不会被调用

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

大家好,感谢您的帮助!我将从代码开始
所以,带有广播接收器的类是这样的:

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);
}
// ....
}

然后我尝试从 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);

但是广播接收器永远不会被调用。我可以这样说是因为 logcat:Log.w("broadcast receiver", "action: "+ action);永远不会被调用。我该如何解决?

谢谢!

编辑:类代码:

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...
}
}

最佳答案

没有看到服务的代码以及它是如何启动的,很难判断问题出在哪里..

首先,您在服务的 onCreate 中注册接收器,但没有注销事件。您应该将广播接收器放在服务类的全局范围内,并在 onDestroy() 中注销(覆盖 onDestroy 和记录一条消息以确认其停止)

其次,除非您绑定(bind)到该服务,否则该服务将在启动命令执行后立即“停止”。

您应该覆盖 onStartCommand 并返回 START_STICKY 以保持服务运行。

关于Android BroadcastReceiver 永远不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12114285/

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