1

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

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:行 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...
    }
}
4

2 回答 2

1

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

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

其次,除非您绑定到服务,否则一旦启动中的命令执行,服务就会“停止”。

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

于 2012-08-24T20:30:34.490 回答
0

您没有收到的原因Intent是因为接收器在onCreate(). context.startService()只会将Activity's传递IntentService's onStartCommand()Service到那时(或应该)已经创建了,因此尝试接收将onCreate()永远不会起作用。

更多信息在这里

尝试接收onStartCommand()只会工作一次(猜测)。但是,只有BroadcastReceiveronStartCommand您致电时,才能获得数据startService()BroadcastReceiver由于您无论如何都在调用它,因此只需放弃 the并将数据与Intent传递到Services一起传递就容易得多onStartCommand()

编辑

@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);

startService()

对该方法的每次调用都将导致对目标服务的 onStartCommand(Intent, int, int) 方法的相应调用,具有此处给出的意图。这提供了一种将作业提交给服务的便捷方式,而无需绑定和调用其接口。

于 2012-08-24T21:03:31.170 回答