Android广播接收器从不被调用
大家好,谢谢你们的帮助! 我将从代码开始Android广播接收器从不被调用,android,broadcast,Android,Broadcast,大家好,谢谢你们的帮助! 我将从代码开始 因此,带有广播接收器的类是: public class MyService extends Service { // ... // ACTION public static final String action = "com.mywebsite.myapp.package.class.action"; // ... public void onCreate() { // SET BROADCAS
因此,带有广播接收器的类是:
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);
}
// ....
}
我试着用这种方式从一个片段发送一个广播
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:line Log.w(“广播接收器”,“动作:”+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并记录一条消息以确认其停止) 其次,除非绑定到服务,否则一旦启动中的命令执行,服务就会“停止”
您应该重写onStartCommand并返回START\u STICKY以保持服务运行。如果看不到服务的代码及其启动方式,则很难判断问题出在哪里 首先,您正在服务的onCreate中注册接收者,但没有注销事件。。您应该将广播接收器置于服务类的全局范围内,并在onDestroy()中注销(覆盖onDestroy并记录一条消息以确认其停止) 其次,除非绑定到服务,否则一旦启动中的命令执行,服务就会“停止”
您应该重写onstart命令并返回START\u STICKY以保持服务运行。您没有收到
意图的原因是因为接收器位于onCreate()
中。
context.startService()
只会将活动的意图传递给服务的启动命令()
。服务
那时已经创建(或者应该已经创建),因此尝试在onCreate()
中接收将永远无法工作
更多信息
尝试在onStartCommand()
中接收将只工作一次(猜测)。但是只有在调用startService()
时,在onStartCommand
中使用BroadcastReceiver
才能获取数据。因为您无论如何都要调用它,所以只需丢弃BroadcastReceiver
并将带有意图的数据传递到服务sonStartCommand()
,就可以轻松得多
编辑
@Override
public void onCreate() {
Log.w("MediaPlayerService", "onCreate called");
}
@Override
public void onDestroy() {
Log.w("MediaPlayerService", "onDestroy called");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.w("MediaPlayerService", "onStartCommand called");
String action = intent.getAction();
Log.w("onStartCommand()", "action: " + action);
if (action.equals("prepareAndPlayNewFile")) {
prepareAndPlayNewFile(intent.getStringExtra("absoluteFilePath"));
}
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
那么就这样做:
Intent myIntent = new Intent(getActivity().getApplicationContext(), MediaPlayerService.class);
myIntent.setAction("prepareAndPlayNewFile");
myIntent.putExtra("absoluteFilePath", "/path/");
startService(myIntent);
:
对该方法的每次调用都将导致对目标服务的onStartCommand(Intent,int,int)方法的相应调用,其意图如下所示。这提供了一种将作业提交到服务的方便方法,而无需绑定和调用其接口
您没有收到意图的原因是因为接收者在onCreate()
中。
context.startService()
只会将活动的意图传递给服务的启动命令()
。服务
那时已经创建(或者应该已经创建),因此尝试在onCreate()
中接收将永远无法工作
更多信息
尝试在onStartCommand()
中接收将只工作一次(猜测)。但是只有在调用startService()
时,在onStartCommand
中使用BroadcastReceiver
才能获取数据。因为您无论如何都要调用它,所以只需丢弃BroadcastReceiver
并将带有意图的数据传递到服务sonStartCommand()
,就可以轻松得多
编辑
@Override
public void onCreate() {
Log.w("MediaPlayerService", "onCreate called");
}
@Override
public void onDestroy() {
Log.w("MediaPlayerService", "onDestroy called");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.w("MediaPlayerService", "onStartCommand called");
String action = intent.getAction();
Log.w("onStartCommand()", "action: " + action);
if (action.equals("prepareAndPlayNewFile")) {
prepareAndPlayNewFile(intent.getStringExtra("absoluteFilePath"));
}
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
那么就这样做:
Intent myIntent = new Intent(getActivity().getApplicationContext(), MediaPlayerService.class);
myIntent.setAction("prepareAndPlayNewFile");
myIntent.putExtra("absoluteFilePath", "/path/");
startService(myIntent);
:
对该方法的每次调用都将导致对目标服务的onStartCommand(Intent,int,int)方法的相应调用,其意图如下所示。这提供了一种将作业提交到服务的方便方法,而无需绑定和调用其接口
当您尝试发送广播时,是否确认您的服务正在运行?我猜在你发送广播之前,服务没有注册广播接收器。同意@Bobbake4。在你的服务中添加一些额外的日志记录
以显示它注册了接收器,并在片段中添加一些日志记录
以显示它发送广播。我检查了这两个日志记录,它们都运行了!那么问题出在哪里呢?当您尝试发送广播时,是否确认您的服务正在运行?我猜在你发送广播之前,服务没有注册广播接收器。同意@Bobbake4。在你的服务中添加一些额外的日志记录
以显示它注册了接收器,并在片段中添加一些日志记录
以显示它发送广播。我检查了这两个日志记录,它们都运行了!那么问题出在哪里呢?它返回START\u STICKY!我编辑了我的第一篇文章,其中包含了与问题相关的部分的代码。希望你能帮助我!并提前感谢您:)它会返回START\u STICKY!我编辑了我的第一篇文章,其中包含了与问题相关的部分的代码。希望你能帮助我!我已经试过了,但是没有用。。可能是因为我需要在服务启动后,在服务运行时接收广播。这可能是原因吗?谢谢,但在这一点上为什么要使用广播?啊,我明白你的意思。等等,我将尝试一些方法,或者只传递带有onStartCommand()
提供的Intent
的数据,您无论如何都在调用它,为什么要让它复杂化呢?相应地编辑了我的答案:)但我需要在服务运行时向服务发送信息,而不是一次又一次地启动它:DI已尝试,但不起作用。。可能是因为我需要在服务启动后,在服务运行时接收广播。可能是这个t