12

我正在构建一个使用服务进行播放的音乐播放器。我有一个控制(播放、暂停、下一步、...)服务的活动 UI。当按下下一个或上一个按钮时,我想从我的服务更新 UI。我想传递一个整数值。这是我的代码:

我使用信使进行通信。我的服务中的代码如下所示:

enum Event {
    NextSongChanged, CurrentPlayingSong
};

public synchronized void registerHandler(Messenger m) {
    clients.add(m);
}


private void emit(Event e) {
    for (Messenger m : clients) {
        try {
            m.send(Message.obtain(null, e.ordinal()));
        } catch (RemoteException exception) {
            /* The client must've died */
            clients.remove(m);
        }
    }
}

我在单击下一个按钮时调用的方法:

public synchronized void playnext() {
    reset();
    if(songIndex <  (songsList.size() - 1)) {
        songIndex += 1;
        playSong(songIndex);
    } else {
        songIndex = 0;
        playSong(songIndex);
    }
    emit(Event.NextSongChanged);
}

一旦我触发 NextSongChanged 事件,我想将“songIndex”变量传递到我的活动中。关于如何实现这一目标的任何想法?

我处理事件的活动代码:

private Messenger playerMessenger = new Messenger(new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (MyService.Event.values()[msg.what]) {
            case NextSongChanged:
                //String songTitle = songsList.get(currentSongIndex+1).get("songTitle");
                //currentSongIndex += 1;
                //songTitleLabel.setText(songTitle);
                //updatePlaying();
                break;

            case CurrentPlayingSong:
                break;
        }
    }
});
4

1 回答 1

17

示例代码希望对其他人有所帮助

1.设置Handler(接收消息)和Messenger(通信)MainActivity.java

Messenger msgService;
boolean isBound;

ServiceConnection connection = new ServiceConnection() {

    @Override
    public void onServiceDisconnected(ComponentName name) { isBound = false; }

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        isBound = true;
        msgService = new Messenger(service);
    }
};

public void sendMessage(View view) {
    if (isBound) {
        try {
            Message message = Message.obtain(null, MessangerService.MESSAGE, 1, 1);
            message.replyTo = replyMessenger;

            Bundle bundle = new Bundle();
            bundle.putString("rec", "Hi, you hear me");
            message.setData(bundle);

            msgService.send(message); //sending message to service

        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
//setting reply messenger and handler
Messenger replyMessenger = new Messenger(new HandlerReplyMsg());

class HandlerReplyMsg extends Handler {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        String recdMessage = msg.obj.toString(); //msg received from service
        toast(recdMessage);
    }
}

2.设置服务类MessengerService.java

Messenger replyMessanger;
final static int MESSAGE = 1;

class IncomingHandler extends Handler {

    @Override
    public void handleMessage(Message msg) {

        if (msg.what == MESSAGE) {
            Bundle bundle = msg.getData();
            toast(bundle.getString("rec"));//message received
            replyMessanger = msg.replyTo; //init reply messenger
            doSomething();
        }
    }
}

private void doSomething() {
    // do stuff

    if (replyMessanger != null)
        try {
            Message message = new Message();
            message.obj = "Yes loud and clear";
            replyMessanger.send(message);//replying / sending msg to activity
        } catch (RemoteException e) {
            e.printStackTrace();
        }
}
Messenger messenger = new Messenger(new IncomingHandler());

@Override
public IBinder onBind(Intent intent) {
    return messenger.getBinder();
}

确保您声明了 Service inmanifestbinding/unbindingService in 活动。

于 2015-05-14T10:46:57.343 回答