问题
I have created android wear activity with a small button (A shortcut to activity in my mobile app). Is it possible to send a bundle request to my main app?
If so how to implement the click listener?
Thanks!
回答1:
This can be done using mentioned MessageApi: http://developer.android.com/training/wearables/data-layer/messages.html
You need to initialize and connect with GoogleApiClient. Once you clicks the button you have to get list of nodes and send a message to them. The last step is to read this message fron phone part of app, this can be done by registering proper WearableListenerService. Please see the sample code below.
Wearable part of app:
Please define such Activity on your Wearable part of app:
public class WearableButtonActivity extends Activity {
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wearable_button_activity);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.build();
mGoogleApiClient.connect();
}
public void onButtonClicked(View target) {
if (mGoogleApiClient == null)
return;
final PendingResult<NodeApi.GetConnectedNodesResult> nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient);
nodes.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(NodeApi.GetConnectedNodesResult result) {
final List<Node> nodes = result.getNodes();
if (nodes != null) {
for (int i=0; i<nodes.size(); i++) {
final Node node = nodes.get(i);
// You can just send a message
Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), "/MESSAGE", null);
// or you may want to also check check for a result:
// final PendingResult<SendMessageResult> pendingSendMessageResult = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), "/MESSAGE", null);
// pendingSendMessageResult.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
// public void onResult(SendMessageResult sendMessageResult) {
// if (sendMessageResult.getStatus().getStatusCode()==WearableStatusCodes.SUCCESS) {
// // do something is successed
// }
// }
// });
}
}
}
});
}
}
and attack onButtonClick method to your button in your res/layout/wearable_button_activity.xml file:
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:layout_gravity="center"
android:onClick="onButtonClicked" />
OR just set OnClickListener from code if you like that way:
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onButtonClicked(v);
}
});
Phone part of app:
Then declare DataLayerListenerService on yout phone's manifest:
<service android:name=".DataLayerListenerService" >
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
DataLayerListenerService class:
public class DataLayerListenerService extends WearableListenerService {
@Override
public void onMessageReceived(MessageEvent messageEvent) {
super.onMessageReceived(messageEvent);
if("/MESSAGE".equals(messageEvent.getPath())) {
// launch some Activity or do anything you like
}
}
}
IMPORTANT: Both parts of your app need to have the same package name to contact with each other.
来源:https://stackoverflow.com/questions/24711232/button-click-in-android-wear