我有一个应用程序,可以通过使用 aWearableListenerService
和onPeerConnected
/来检测 android 穿戴设备何时断开连接onPeerDisconnected
。
似乎这些已被弃用,所以我现在正在尝试使用onCapabilityChanged
,但我无法调用此函数。我在我的服务清单中使用它。关于这些功能的文档不是很好。
<intent-filter>
<action android:name="com.google.android.gms.wearable.CAPABILITY_CHANGED" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
所以我终于让它工作了。需要设置一些东西,但我会一一列出。
在 Root build.gradle 文件中有:
ext {
TARGET_SDK_VERSION = 25
VERSION_CODE = 7
VERSION_NAME = '2.0'
COMPILE_SDK_VERSION = 25
BUILD_TOOLS_VERSION = '25.0.2'
APPLICATION_ID = "com.example.projectname"
PLAY_SERVICES_WEARABLE = 'com.google.android.gms:play-services-wearable:9.4.0'
}
Run Code Online (Sandbox Code Playgroud)
在每个模块 build.gradle 文件中,可以参考如下所示:
apply plugin: 'com.android.application'
android {
compileSdkVersion rootProject.ext.COMPILE_SDK_VERSION
buildToolsVersion rootProject.ext.BUILD_TOOLS_VERSION
defaultConfig {
applicationId rootProject.ext.APPLICATION_ID
minSdkVersion 20
targetSdkVersion rootProject.ext.TARGET_SDK_VERSION
versionCode rootProject.ext.VERSION_CODE
versionName rootProject.ext.VERSION_NAME
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
provided 'com.google.android.wearable:wearable:2.0.1'
compile 'com.google.android.support:wearable:2.0.1'
compile rootProject.ext.PLAY_SERVICES_WEARABLE
}
Run Code Online (Sandbox Code Playgroud)
WearableListenerService
现在必须intent-filter
为安卓系统调用的每个覆盖函数定义一个。在onCapabilityChanged
函数的情况下,意图过滤器应定义为:ext {
TARGET_SDK_VERSION = 25
VERSION_CODE = 7
VERSION_NAME = '2.0'
COMPILE_SDK_VERSION = 25
BUILD_TOOLS_VERSION = '25.0.2'
APPLICATION_ID = "com.example.projectname"
PLAY_SERVICES_WEARABLE = 'com.google.android.gms:play-services-wearable:9.4.0'
}
Run Code Online (Sandbox Code Playgroud)
在intent-filter
为onCapabilityChanged
是com.google.android.gms.wearable.CAPABILITY_CHANGED
。除此之外,意图过滤器还需要被告知数据方案和主机。这可以简单地data android:scheme="wear" android:host="*"
。将pathPrefix
可以为这个意图过滤器可以省略。请注意,意向过滤器的com.google.android.gms.wearable.DATA_CHANGED
,并com.google.android.gms.wearable.MESSAGE_RECEIVED
需要pathPrefix
定义为能有自己称为服务各自的功能。
onCapabilityChanged
启动该功能,系统需要检测具有连接能力的设备。为此,我们必须在每个模块的 xml 文件中定义功能。为此,在每个模块中,保存wear.xml
在 res/values 目录中命名的文件。该文件必须有一个android_wear_capabilities
以项目命名的字符串数组,这些项目描述了您希望您的模块向另一个设备通告的功能。以下是wear.xml
可穿戴模块中包含的文件示例。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="android_wear_capabilities">
<item>verify_remote_wear_app</item>
</string-array>
</resources>
Run Code Online (Sandbox Code Playgroud)
首先,需要注意的是,文件必须被命名wear.xml
并且必须放在值目录中。其次,字符串数组必须命名为android_wear_capabilities
。还要确保每个模块中的每个功能都有唯一的名称。
如果上述任何一项不正确,那么该onCapabilityChanged
函数将永远不会被调用,您将沮丧地拔毛。
现在,要实际判断设备是否已断开连接,请使用以下onCapabilityChanged
函数:
public void onCapabilityChanged(CapabilityInfo capabilityInfo) {
super.onCapabilityChanged(capabilityInfo);
if(capabilityInfo.getNodes().size() > 0){
Log.d(TAG, "Device Connected");
}else{
Log.d(TAG, "No Devices");
}
}
Run Code Online (Sandbox Code Playgroud)
此功能会告诉您设备何时连接或断开连接,假设一次仅连接 1 个设备。
归档时间: |
|
查看次数: |
629 次 |
最近记录: |