-
Notifications
You must be signed in to change notification settings - Fork 1
Receive_en
WearTools also provides listeners. You can register them anywhere, and it will be running at the same thread, so you can refresh your UI with return calls.
Warning! You have to implement carefully, if you are going to instantiate your listener in Activity, to prevent memory leak. For more information, please see Memory Leak.
Instantiate and register WTMessageListener to listen Message. Here's a demo.
WTMessageListener messageListener = new WTMessageListener() {
@Override
public void onMessageReceived(String nodeId, String path, byte[] data, byte[] bothwayId) {
Log.i(TAG, "Receive msg: " + new String(data));
}
};
WTRegister.addMessageListener(context, messageListener); // Register
//WTRegister.removeMessageListener(context, messageListener); // RemoveInstantiate and register WTMessageListener to listen to Message. Here's a demo.
WTDataListener dataListener = new WTDataListener() {
@Override
public void onDataChanged(String path, DataMap dataMap) {
// Respond to WTSender.sendData()
Log.i(TAG, "Receive data: " + dataMap.getString("test"));
}
@Override
public void onDataDeleted(String path) {
// Respond to WTSender.deleteData()
Log.i(TAG, "Data deleted: " + path);
}
};
WTRegister.addDataListener(context, dataListener); // Register
//WTRegister.removeDataListener(context, dataListener); // RemoveAs mentioned before, we can use Asset to transmit big file. WearTools provides func AssetHelper.get() to get your data. Here's a demo, showing how to get the pic, which is sent in the previous doc, and present on screen.
@Override
public void onDataChanged(String path, DataMap dataMap) {
super.onDataChanged(path, dataMap);
Log.i(TAG, "Receive image");
Asset asset = dataMap.getAsset("image");
AssetHelper.get(context, asset, new AssetHelper.AssetCallback() {
@Override
public void onResult(InputStream ins) {
imageView.setImageBitmap(BitmapFactory.decodeStream(ins));
}
});
}Sometimes we only need to process data without UI interaction. For this circumstance, we use Service to listen.
Please create a class which is inherited from WTListenerService. This Service's life circle will be managed automatically by the system, and you should not start or stop it manually. It will be called automatically when it receives data. Here's a demo.
public class ListenerService extends WTListenerService {
@Override
public void onMessageReceived(String nodeId, String path, byte[] data, byte[] bothwayId) {
}
@Override
public void onDataChanged(String path, DataMap dataMap) {
}
@Override
public void onDataDeleted(String path) {
}
}You have to register Service in manifest, and add specific <intent-filter>, with specific action and data. Otherwise, the service will not be executed. For more information about <intent-filter>, please visit Google Doc.
Notice: All com.google.android.gms.wearable prefix should be replaced as com.mobvoi.android.wearable.
Here's a demo, which has registered the service above.
<service android:name="cc.chenhe.lib.weartools.demo.ListenerService">
<intent-filter>
<action android:name="com.mobvoi.android.wearable.MESSAGE_RECEIVED" />
<action android:name="com.mobvoi.android.wearable.DATA_CHANGED" />
<data
android:host="*"
android:path="/image"
android:scheme="wear" />
<data
android:host="*"
android:path="/data/test"
android:scheme="wear" />
<data
android:host="*"
android:path="/msg/test"
android:scheme="wear" />
</intent-filter>
</service>Notice: Used BIND_LISTENER action is no more available. When you register BIND_LISTENER, you will not get any return call.