android - WearableListenerService onCreate() 从未调用过

标签 android wear-os android-wear-2.0

最近我的一个支持 Wear OS (Android Wear) 的应用程序出现了一个错误,当可穿戴设备向它发送数据时,移动设备上的服务永远不会使用react。

在排查根本原因时,我注意到从未调用 WearableListenerServiceonCreate() 方法。同样,从未调用 Service 父类(super class)的 onStartCommand() 方法,我认为这意味着该 Service 确实没有运行。

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

下面是一个新项目的一些代码 fragment ,我所做的就是定义一个 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 上进行了测试。

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

最佳答案

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

<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 结尾(您的 list 有MESSAGE_LISTENER)。我认为这是关键问题,但导出服务并添加 pathPrefix 可能也是个好主意。

关于android - WearableListenerService onCreate() 从未调用过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50844792/

相关文章:

java - 确定 GPS 位置(纬度/经度)是否在固定区域内... [Android]

android - 无法从动态添加的复选框中获取值

android - Kotlin协程是否在应用程序所在的进程中运行?

android - Android Wear 2.0 和 SmartWatch Android 5.1 之间的区别

android - 如何在 Android Wear Complication Provider 的表盘上绘制图标?

java - 如何滑动主布局并强制其调整大小?

android - 从磨损接收数据后写.wav为空

Android Wear Preview 堆叠式通知

java - 在 WearOS 中显示多个通知

java - 安卓磨损 : Layout is not displaying properly in Emulator