Scroll Essential Android 2.3 Manual de usuario Pagina 32

  • Descarga
  • Añadir a mis manuales
  • Imprimir
  • Pagina
    / 47
  • Tabla de contenidos
  • MARCADORES
  • Valorado. / 5. Basado en revisión del cliente
Vista de pagina 31
Android Wear Docs, Release 1.1
else {
// Log an error
Log.v("myTag", "ERROR: failed to send Message");
}
}
}
}
6.1.2 Implement a Message Listener
You can monitor the data layer for new messages using either a listener service or listener activity. This section
explains how to implement a listener service for messages. For the wearable, modify the code in the wear branch of
the Android Studio project.
Add Build Dependencies
Add wearable SDK dependencies to the build.gradle file (Module:wear) in the Gradle Scripts folder, as necessary.
dependencies {
compile fileTree(dir: ’libs’, include: [’
*
.jar’])
compile ’com.google.android.support:wearable:1.1.+’
compile ’com.google.android.gms:play-services-wearable:6.5.+’
}
Add Listener Service to Manifest
Enable the data layer listener in the manifest file.
<uses-feature android:name="android.hardware.type.watch" />
<application
...
<service android:name=".ListenerService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
</application>
Create a Listener Service Class
Create a listener in the wear application that extends the WearableListenerService. This example logs any received
message to the debug output.
public class ListenerService extends WearableListenerService {
@Override
public void onMessageReceived(MessageEvent messageEvent) {
if (messageEvent.getPath().equals("/message_path")) {
final String message = new String(messageEvent.getData());
Log.v("myTag", "Message path received on watch is: " + messageEvent.getPath());
Log.v("myTag", "Message received on watch is: " + message);
28 Chapter 6. Data Layer Messages
Vista de pagina 31
1 2 ... 27 28 29 30 31 32 33 34 35 36 37 ... 46 47

Comentarios a estos manuales

Sin comentarios