【问题标题】:WearableListenerService onCreate() never calledWearableListenerService onCreate() 从未调用
【发布时间】:2018-06-13 19:23:21
【问题描述】:

最近在我的一个支持 Wear OS (Android Wear) 的应用中出现了一个错误,即当可穿戴设备向其发送数据时,移动设备上的服务永远不会做出反应。

在解决根本原因时,我注意到 WearableListenerServiceonCreate() 方法从未被调用。同样,Service 超类中的 onStartCommand() 方法永远不会被调用,我认为这意味着 Service 确实没有运行。

Service 是在 AndroidManifest.xml 中定义的,所以我不明白为什么它不再运行了。

以下是来自一个新项目的一些代码 sn-ps,我所做的只是定义一个 WearableListenerService 并尝试启动它。请注意,这是定义一个在移动设备上运行的服务:

WearMessageListenerService.java

package com.example.test.wearlistenertest;

import android.content.Intent;
import android.util.Log;

import com.google.android.gms.wearable.WearableListenerService;

public class WearMessageListenerService extends WearableListenerService {
    @Override
    public void onCreate() {
        super.onCreate();

        Log.v("test", "onCreate()");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.v("test", "onStartCommand()");

        return super.onStartCommand(intent, flags, startId);
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test.wearlistenertest">

    <application
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="com.example.test.wearlistenertest.WearMessageListenerService">
            <intent-filter>
                <action android:name="com.google.android.gms.wearable.MESSAGE_LISTENER" />
                <data android:scheme="wear" android:host="*" />
            </intent-filter>
        </service>
    </application>
</manifest>

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.test.wearlistenertest"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    compile 'com.google.android.gms:play-services-wearable:11.8.0'
}

上述 Log.v() 方法都不会在运行时输出任何内容。我已经在 API 23 和 27 上进行了测试。

非常感谢您对此提供的帮助,因为我不明白为什么我无法启动基本服务。我很乐意提供任何其他详细信息。非常感谢您提供的任何帮助。 - 亚伦

【问题讨论】:

  • 您是否收到了来自手表应用的消息?如果不是,那么这与onCreate() 方法无关,也许您应该发布用于从手表发送消息的代码。

标签: android wear-os android-wear-2.0


【解决方案1】:

在我看来,您对消息接收者的清单声明是错误的。这是我的一个应用程序中的一个:

<service android:name=".MyMessageListener"
    android:exported="true">
    <intent-filter>
        <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
        <data android:host="*" android:scheme="wear"
              android:pathPrefix="@string/wear_path_prefix"/>
    </intent-filter>
</service>

具体来说,请注意action 应以MESSAGE_RECEIVED 结尾(您的清单有MESSAGE_LISTENER)。我认为这是关键问题,但导出服务并添加pathPrefix 可能也是个好主意。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    • 1970-01-01
    • 1970-01-01
    • 2016-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多