Scroll Essential Android 2.3 Manual de usuario Pagina 33

  • Descarga
  • Añadir a mis manuales
  • Imprimir
  • Pagina
    / 47
  • Tabla de contenidos
  • MARCADORES
  • Valorado. / 5. Basado en revisión del cliente
Vista de pagina 32
Android Wear Docs, Release 1.1
}
else {
super.onMessageReceived(messageEvent);
}
}
}
6.1.3 Display Received Messages
The wearable listener service cannot directly update the wearable UI because it runs on a different thread. The
following example shows how to forward received messages to the main Activity using the LocalBroadcastManager.
1. In the ListenerService class, broadcast the received data layer messages locally.
@Override
public void onMessageReceived(MessageEvent messageEvent) {
if (messageEvent.getPath().equals("/message_path")) {
final String message = new String(messageEvent.getData());
// Broadcast message to wearable activity for display
Intent messageIntent = new Intent();
messageIntent.setAction(Intent.ACTION_SEND);
messageIntent.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(messageIntent);
}
else {
super.onMessageReceived(messageEvent);
}
2. In the wearable Activity, register to receive broadcasts from the ListenerService.
@Override
protected void onCreate(Bundle savedInstanceState) {
// Basic UI code, generated by New Project wizard.
...
// Register the local broadcast receiver, defined in step 3.
IntentFilter messageFilter = new IntentFilter(Intent.ACTION_SEND);
MessageReceiver messageReceiver = new MessageReceiver();
LocalBroadcastManager.getInstance(this).registerReceiver(messageReceiver, messageFilter);
}
3. In the wearable Activity, define a nested class that extends BroadcastReceiver, implements the onReceive
method, and extracts the message. This example receives and displays the message the wearable UI.
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("message");
// Display message in UI
mTextView.setText(message);
}
}
}
Keep in mind that this example is not a full implementation. You must unregister your application from the local
broadcast manager when the application stops. Otherwise you can duplicate the registration of the same application,
6.1. First Wearable Message 29
Vista de pagina 32
1 2 ... 28 29 30 31 32 33 34 35 36 37 38 ... 46 47

Comentarios a estos manuales

Sin comentarios