简体   繁体   English

WearableListenerService onMessageReceived 不起作用

[英]WearableListenerService onMessageReceived not working

I am sending messages from my phone to the Wear.我正在从手机向 Wear 发送消息。 I want to be able to receive messages even when the wear activity is closed.我希望即使在磨损活动关闭时也能收到消息。

I wrote a small service which launches my main Activity (UI) if it receives a message.我写了一个小服务,如果它收到一条消息,它会启动我的主要活动(UI)。 However, the onMessageReceived function never gets called in the WearableListenerService.但是,在 WearableListenerService 中永远不会调用 onMessageReceived 函数。 I also tried the sample code at this tutorial , but it doesn't seem to work for me.我还尝试了本教程中的示例代码,但它似乎对我不起作用。

What I have tried so far:到目前为止我尝试过的:

  1. Other threads posted on SO with the same problem mentioned issues with applicationId in mobile and wear module.在 SO 上发布的具有相同问题的其他线程提到了移动和穿戴模块中 applicationId 的问题。 The applicationId is the same in both modules.两个模块中的 applicationId 相同。
  2. I tried writing the messageReceived function within the Activity, and it works as long as the activity is open (which is the expected behavior).我尝试在活动中编写 messageReceived 函数,只要活动处于打开状态(这是预期的行为),它就可以工作。
  3. I know that I don't have to launch the WearableListenerService, it automatically gets started upon receiving a message, but I explicitly tried launching it from the activity, as a last attempt.我知道我不必启动 WearableListenerService,它会在收到消息后自动启动,但作为最后一次尝试,我明确尝试从 Activity 启动它。 The service does get created, but the onMessageReceived() function doesn't work.该服务确实被创建,但 onMessageReceived() 函数不起作用。 It doesn't even reach the function (tested by putting logs).它甚至没有达到该功能(通过放置日志进行测试)。
  4. I also tried making it a regular background service (as you would in Android), copy the communication part to service, but to no avail.我还尝试将其设为常规后台服务(就像在 Android 中一样),将通信部分复制到服务,但无济于事。

Here is the code:这是代码:

Wear Manifest:磨损清单:

  <service android:name=".MessageService">
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
            <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
            <action android:name="com.google.android.gms.wearable.CAPABILITY_CHANGED" />
            <action android:name="com.google.android.gms.wearable.CHANNEL_EVENT" />
            <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
            <data android:scheme="wear" android:host="*" android:pathPrefix="/prefix" />
        </intent-filter>
    </service>

MessageService:消息服务:

public class MessageService extends WearableListenerService {
public Handler h;
@Override
public void onCreate() {

    Log.i("service", "im here");

}
@Override
public void onMessageReceived(MessageEvent messageEvent) {
    Log.i("onMessageReceived","im here!");
        Intent intent = new Intent( this, MainActivity.class );
        intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
        startActivity( intent );

       }

The code the Main Activity in Wear and the main activity on mobile is from this example. Wear 中的 Main Activity 和移动设备上的 main Activity 的代码来自此示例。 . .

The communication within the activity works, the service is not working.活动内的通信有效,服务无效。 Help?帮助?

The message path in the mobile app has to match that in the intent filter of the service.移动应用程序中的消息路径必须与服务的意图过滤器中的路径相匹配。 You have this as '/prefix' in the intent filter but is 'message1' or 'message2' in the mobile app.您在意图过滤器中将此作为“/前缀”,但在移动应用程序中为“message1”或“message2”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM