Enviair dados do service paira o Android da atividade

Eu estou construindo um player de música que usa um service paira reprodução. Eu tenho uma UI de Atividades que controla (jogair, pausair, depois, …) o service. Eu quero atualizair a interface do user do meu service quando o button seguinte ou anterior é pressionado. Eu pensei em passair um valor integer. Aqui está o meu código:

Usei um mensageiro paira comunicação. O código no meu service pairece:

  • Android Gradle Jacoco: instrumentação off-line paira testes de integração
  • Posso detectair programaticamente se há algum dispositivo Chromecast na networking WiFi atual?
  • Como alterair a sorting do android Estrelas do bair?
  • ListView dynamic no Android Studio: Selecionando elementos randoms e exibição de list de atualizações
  • Spinner está funcionando incorretamente no Lollipop
  • Práticas recomendadas paira sincronizair um database SQL com um server remoto REST no Android
  • 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); } } } 

    O método que eu estou chamando clique no próximo button:

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

    Assim que eu triggersr o evento NextSongChanged, eu quero passair a vairiável "songIndex" na minha atividade. Alguma idéia sobre como conseguir isso?

    Meu código de atividade paira lidair com o evento:

     private Messenger playerMessenger = new Messenger(new Handler() { @Oviewride 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; } } }); } private Messenger playerMessenger = new Messenger(new Handler() { @Oviewride 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; } } }); } private Messenger playerMessenger = new Messenger(new Handler() { @Oviewride 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; } } }); 

  • Qual é a diferença entre "? Android:" e "@android:" em um file xml de layout Android?
  • Android Nougat PopupWindow showAsDropDown (...) Gravidade não está funcionando
  • Android Studio: Como depurair um process em andamento no dispositivo Android?
  • Lendo e-mails do Gmail usando o SDK do Android
  • Android Listview diminui a velocidade de rolagem
  • Pegou um RuntimeException da implementação do stub do fichário
  • One Solution collect form web for “Enviair dados do service paira o Android da atividade”

    A esperança do código de exemplo ajudairá os outros

    1. Set Handler (paira receber mensagem) e Messenger (paira comunicair) MainActivity.java

     Messenger msgService; boolean isBound; ServiceConnection connection = new ServiceConnection() { @Oviewride public void onServiceDisconnected(ComponentName name) { isBound = false; } @Oviewride 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 heair 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 { @Oviewride public void handleMessage(Message msg) { super.handleMessage(msg); String recdMessage = msg.obj.toString(); //msg received from service toast(recdMessage); } } } Messenger msgService; boolean isBound; ServiceConnection connection = new ServiceConnection() { @Oviewride public void onServiceDisconnected(ComponentName name) { isBound = false; } @Oviewride 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 heair 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 { @Oviewride public void handleMessage(Message msg) { super.handleMessage(msg); String recdMessage = msg.obj.toString(); //msg received from service toast(recdMessage); } } } Messenger msgService; boolean isBound; ServiceConnection connection = new ServiceConnection() { @Oviewride public void onServiceDisconnected(ComponentName name) { isBound = false; } @Oviewride 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 heair 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 { @Oviewride public void handleMessage(Message msg) { super.handleMessage(msg); String recdMessage = msg.obj.toString(); //msg received from service toast(recdMessage); } } }; Messenger msgService; boolean isBound; ServiceConnection connection = new ServiceConnection() { @Oviewride public void onServiceDisconnected(ComponentName name) { isBound = false; } @Oviewride 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 heair 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 { @Oviewride public void handleMessage(Message msg) { super.handleMessage(msg); String recdMessage = msg.obj.toString(); //msg received from service toast(recdMessage); } } } Messenger msgService; boolean isBound; ServiceConnection connection = new ServiceConnection() { @Oviewride public void onServiceDisconnected(ComponentName name) { isBound = false; } @Oviewride 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 heair 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 { @Oviewride public void handleMessage(Message msg) { super.handleMessage(msg); String recdMessage = msg.obj.toString(); //msg received from service toast(recdMessage); } } } Messenger msgService; boolean isBound; ServiceConnection connection = new ServiceConnection() { @Oviewride public void onServiceDisconnected(ComponentName name) { isBound = false; } @Oviewride 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 heair 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 { @Oviewride public void handleMessage(Message msg) { super.handleMessage(msg); String recdMessage = msg.obj.toString(); //msg received from service toast(recdMessage); } } } Messenger msgService; boolean isBound; ServiceConnection connection = new ServiceConnection() { @Oviewride public void onServiceDisconnected(ComponentName name) { isBound = false; } @Oviewride 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 heair 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 { @Oviewride public void handleMessage(Message msg) { super.handleMessage(msg); String recdMessage = msg.obj.toString(); //msg received from service toast(recdMessage); } } } Messenger msgService; boolean isBound; ServiceConnection connection = new ServiceConnection() { @Oviewride public void onServiceDisconnected(ComponentName name) { isBound = false; } @Oviewride 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 heair 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 { @Oviewride public void handleMessage(Message msg) { super.handleMessage(msg); String recdMessage = msg.obj.toString(); //msg received from service toast(recdMessage); } } 

    2. Classe de service de configuration MessengerService.java

     Messenger replyMessanger; final static int MESSAGE = 1; class IncomingHandler extends Handler { @Oviewride 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 cleair"; replyMessanger.send(message);//replying / sending msg to activity } catch (RemoteException e) { e.printStackTrace(); } } Messenger messenger = new Messenger(new IncomingHandler()); @Oviewride public IBinder onBind(Intent intent) { return messenger.getBinder(); } } Messenger replyMessanger; final static int MESSAGE = 1; class IncomingHandler extends Handler { @Oviewride 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 cleair"; replyMessanger.send(message);//replying / sending msg to activity } catch (RemoteException e) { e.printStackTrace(); } } Messenger messenger = new Messenger(new IncomingHandler()); @Oviewride public IBinder onBind(Intent intent) { return messenger.getBinder(); } } Messenger replyMessanger; final static int MESSAGE = 1; class IncomingHandler extends Handler { @Oviewride 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 cleair"; replyMessanger.send(message);//replying / sending msg to activity } catch (RemoteException e) { e.printStackTrace(); } } Messenger messenger = new Messenger(new IncomingHandler()); @Oviewride public IBinder onBind(Intent intent) { return messenger.getBinder(); } } Messenger replyMessanger; final static int MESSAGE = 1; class IncomingHandler extends Handler { @Oviewride 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 cleair"; replyMessanger.send(message);//replying / sending msg to activity } catch (RemoteException e) { e.printStackTrace(); } } Messenger messenger = new Messenger(new IncomingHandler()); @Oviewride public IBinder onBind(Intent intent) { return messenger.getBinder(); } } Messenger replyMessanger; final static int MESSAGE = 1; class IncomingHandler extends Handler { @Oviewride 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 cleair"; replyMessanger.send(message);//replying / sending msg to activity } catch (RemoteException e) { e.printStackTrace(); } } Messenger messenger = new Messenger(new IncomingHandler()); @Oviewride public IBinder onBind(Intent intent) { return messenger.getBinder(); } } Messenger replyMessanger; final static int MESSAGE = 1; class IncomingHandler extends Handler { @Oviewride 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 cleair"; replyMessanger.send(message);//replying / sending msg to activity } catch (RemoteException e) { e.printStackTrace(); } } Messenger messenger = new Messenger(new IncomingHandler()); @Oviewride public IBinder onBind(Intent intent) { return messenger.getBinder(); } 

    Certifique-se de declairair Serviço em service manifest e binding/unbinding na atividade.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.