我正在玩一些可穿戴设备,我已经从我的微型应用程序(在可穿戴设备上运行)创建了通知with some little restrictions我想知道如何为在手机上打开主应用程序的操作创建待定 Intent 。
最佳答案
不知道有没有别的办法。
但是它可以工作。
您在磨损模块中构建一个通知,该通知会在磨损中启动广播。
广播(在穿戴设备上运行)使用 Message.API 向移动模块发送消息。
在移动模块上有一个 WearableListenerService
,它在移动设备上启动 MainActivity。
在您的 Wear 中构建通知:
// 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;
}
此通知启动广播消息。 在你的 wear/AndroidManifest.xml 中声明这个广播
穿/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>
然后实现广播发送消息:
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
}
}
此代码将向您的手机发送一条消息。 它需要在你的 wear/build.gradle
dependencies {
compile "com.google.android.support:wearable:1.0.+"
compile 'com.google.android.gms:play-services-wearable:+'
}
在**移动模块上你必须实现一个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);
}
}
}
需要在你的Mobile/AndroidManifest.xml中声明Service。
<service android:name=".ListenerServiceFromWear">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
和这个mobile/build.gradle依赖:
dependencies {
wearApp project(':wear')
compile 'com.google.android.gms:play-services-wearable:+'
}
关于android - 如何从可穿戴设备打开主应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25885233/