服务:从内部静态广播接收消息

时间:2017-11-18 20:03:25

标签: android service broadcastreceiver

当我按下耳机按钮时,我想在我的服务类中调用一个函数。因此,我需要在我的服务中实现静态内部广播类。但是现在我无法访问当前正在运行的MediaPlayer的本地字段,因为我必须保持静态。我用可观察的方式尝试了它,但也没有用。 Like in this answer

我还尝试将字段本身设置为静态,但在我想要实现的逻辑中,我需要一个切换歌曲的功能,所以我必须将播放列表设置为静态,这对我来说是不可接受的。

public class MediaService extends Service implements
    MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener,
    MediaPlayer.OnCompletionListener {

private IntentFilter intentFilterMediaButton = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
private MediaButtonReceiver mMediaButtonReceiver = new MediaButtonReceiver();

public MediaPlayer player;

@Override
public void onPrepared(MediaPlayer mediaPlayer) {
    registerReceiver(this.mMediaButtonReceiver, this.intentFilterMediaButton);
    player.start();
}

static int xTime = 0;
public static class MediaButtonReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {
        String intentAction = intent.getAction();
        if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
            return;
        }
        KeyEvent event =   (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return;
        }
        int action = event.getAction();
        if (action == KeyEvent.ACTION_DOWN) {
            switch (action){
                case 0: // Button down
                    xTime++;
                    Handler handler = new Handler();
                    Runnable run = new Runnable() {
                        @Override
                        public void run() {
                            if(xTime == 1)
                                //MediaService.mediaControlAction(xTime);
                            if(xTime == 2)
                                //MediaService.mediaControlAction(xTime);
                            xTime = 0;
                        }
                    };
                    if(xTime == 1)
                        handler.postDelayed(run, 500);
                    break;
            }
        }
        abortBroadcast();
    }
}

}

1 个答案:

答案 0 :(得分:0)

Eventbus

我在服务中添加了以下内容

private EventBus eventBus = EventBus.getDefault();
this.eventBus.register(this);

@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onEvent(MediaActionEvent event){
    Toast.makeText(this, String.valueOf(event.getxTimesPressed()) + " times pressed", Toast.LENGTH_SHORT).show();
}

在我的广播中调用事件

Runnable run = new Runnable() {
                        @Override
                        public void run() {
                            if(xTime == 1)
                                EventBus.getDefault().post(new MediaActionEvent(xTime));
                            if(xTime == 2)
                                EventBus.getDefault().post(new MediaActionEvent(xTime));

并为活动创建了一个新类

public class MediaActionEvent {
private final int xTimesPressed;

public MediaActionEvent(int xTimesPressed) {
    this.xTimesPressed = xTimesPressed;
}

public int getxTimesPressed() {
    return xTimesPressed;
} 
}