【问题标题】:How to open the main app from the wearable?如何从可穿戴设备打开主应用程序?
【发布时间】:2014-11-11 04:11:36
【问题描述】:

我正在使用可穿戴设备,我已经从我的微型应用程序(在可穿戴设备上运行with some little restrictions 创建了通知,我想知道如何为在手机上打开主应用的操作。

【问题讨论】:

    标签: android android-intent android-notifications android-pendingintent wear-os


    【解决方案1】:

    不知道有没有其他办法。

    但是它可以工作。 你在你的穿戴模块中构建一个通知,然后在穿戴中启动一个广播。 广播(在穿戴设备上运行)使用 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 中声明这个广播

    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>
    

    然后实现Broadcast发送消息:

    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:+'
    }
    

    在 **Mobile 模块上你必须实现 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 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:+'
    }
    

    【讨论】:

    • 这怎么可能?我无法从可穿戴设备访问 MainActivity 类,这是一个不同的 apk 文件。
    • 您可以在移动应用中创建通知。您是否只在 Wear 应用中创建通知?
    • 是的,在这种特殊情况下,我正在谈论为可穿戴设备可穿戴设备创建通知。
    • 好的,在这种情况下,您必须使用发送消息的 Intent(使用 MessageAPI Wearable.MessageApi.sendMessage)。在移动应用中,您必须实现一个 WearableListenerService 来监听消息并启动 MainActivity。
    • 所以我也可以在手机上实现WearableListenerService?我不确定这一点。
    【解决方案2】:

    我刚刚测试过并且有效的解决方案是在 Android 应用程序上添加一个消息侦听器,然后简单地从可穿戴设备发送我想要它执行的操作的详细信息。

    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);
            }
        }
    }
    

    不要忘记将其添加到您的清单中:

    <service android:name=".wearable.WearMessagesAPI_Service">
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.BIND_LISTENER"/>
        </intent-filter>
    </service>
    

    我相信就是这样。 让我知道进展如何:)

    【讨论】:

    • 啊,我现在想起来我的问题了。 WearableListenerService 是播放服务可穿戴库的一部分。您确定可以在普通手机上使用它吗?那么 minSdkVersion 是 9,所以它可以工作。
    • 最低 API 20 是在可穿戴设备上运行可穿戴设备。如果您使用包含一些可穿戴类的 com.google.android.gms:play-services:6.1.71 库,则可以在手机应用中使用 WearableListenerService .
    • @TacB0sS 你有示例项目吗?我遇到了代码问题。这个条件应该是什么? activityKey.equals(...)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多