I'm playing a little with wearables, I already created notifications from my micro app (which runs on the wearable) with some little restrictions and I'm wondering how I can create a pending intent for an action to open the main app on the phone.
问题:
回答1:
My solution that I've just tested and it works is add a message listener on the Android application and simply send from the wearable the details of what I want it to do.
public class WearMessagesAPI_Service
extends WearableListenerService {
private static final String OPEN_APP_PATH = "/OpenApp";
@Override
public void onMessageReceived(MessageEvent event) {
Log.w("WearMessagesAPI_Service", event.getPath());
String activityKey = new String(event.getData());
if(activityKey.equals(...)) {
Intent myAppIntent = ... create intent ...
startActivity(myAppIntent);
}
}
}
Don't forget to add it to your manifest:
<service android:name=".wearable.WearMessagesAPI_Service">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER"/>
</intent-filter>
</service>
And I believe that is it. Let me know how it goes :)
回答2:
I don't know if there is another way.
However it can work.
You build a notification in your wear module whick launches a Broadcast in the wear.
The broadcast (which runs on the wear) uses the Message.API to send a message to the mobile module.
On the mobile module there is a WearableListenerService
which launches the MainActivity on mobile.
In your Wear build the notification:
// Notification
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
//.setContentTitle(mNotificationUtil.getTitle())
.setContentText("Text")
.setContentIntent(getPendingIntent(this));
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
// Build the notification and issues it with notification manager.
notificationManager.notify(1, notificationBuilder.build());
}
private PendingIntent getPendingIntent(MainActivity mainActivity) {
final String INTENT_ACTION = "it.gmariotti.receiver.intent.action.TEST";
Intent intent = new Intent();
intent.setAction(INTENT_ACTION);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
return pendingIntent;
}
This notification launches a Broadcast message. Declare this broadcast in your wear/AndroidManifest.xml
wear/AndroidManifes.xml
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<receiver android:name=".MyBroadcast">
<intent-filter>
<action android:name="it.gmariotti.receiver.intent.action.TEST"/>
</intent-filter>
</receiver>
Then implement the Broadcast to send the Message:
public class MyBroadcast extends BroadcastReceiver implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
Node mNode; // the connected device to send the message to
GoogleApiClient mGoogleApiClient;
private static final String WEAR_PATH = "/hello-world-wear";
@Override
public void onReceive(Context context, Intent intent) {
//Connect the GoogleApiClient
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
/**
* Send message to mobile handheld
*/
private void sendMessage() {
if (mNode != null && mGoogleApiClient!=null && mGoogleApiClient.isConnected()) {
Wearable.MessageApi.sendMessage(
mGoogleApiClient, mNode.getId(), WEAR_PATH, null).setResultCallback(
new ResultCallback<MessageApi.SendMessageResult>() {
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult) {
if (!sendMessageResult.getStatus().isSuccess()) {
Log.e("TAG", "Failed to send message with status code: "
+ sendMessageResult.getStatus().getStatusCode());
}
}
}
);
}else{
//Improve your code
}
}
/*
* Resolve the node = the connected device to send the message to
*/
private void resolveNode() {
Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
@Override
public void onResult(NodeApi.GetConnectedNodesResult nodes) {
for (Node node : nodes.getNodes()) {
mNode = node;
}
sendMessage();
}
});
}
@Override
public void onConnected(Bundle bundle) {
resolveNode();
}
@Override
public void onConnectionSuspended(int i) {
//Improve your code
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
//Improve your code
}
}
This code will send a Message to your mobile phone. It requires in your wear/build.gradle
dependencies {
compile "com.google.android.support:wearable:1.0.+"
compile 'com.google.android.gms:play-services-wearable:+'
}
On the **Mobile module you have to implement a WearableListenerService
**
/**
* @author Gabriele Mariotti (gabri.mariotti@gmail.com)
*/
public class ListenerServiceFromWear extends WearableListenerService {
private static final String WEAR_PATH = "/hello-world-wear";
@Override
public void onMessageReceived(MessageEvent messageEvent) {
/*
* Receive the message from wear
*/
if (messageEvent.getPath().equals(WEAR_PATH)) {
Intent startIntent = new Intent(this, MainActivity.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startIntent);
}
}
}
It requires in your Mobile/AndroidManifest.xml to declare the Service.
<service android:name=".ListenerServiceFromWear">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
and this mobile/build.gradle dependency:
dependencies {
wearApp project(':wear')
compile 'com.google.android.gms:play-services-wearable:+'
}