如何点击安卓配套APP按钮直接启动WearOS设备上的指定应用?
问题描述
需要实现:点击安卓手机配套APP的按钮,直接启动已配对WearOS手表上的指定应用(如app-1),无需在WearOS端额外部署处理消息的应用。
此前尝试过Wearable APIs、Android Device Policy Controller (DPC) APIs,但这类方案都需要WearOS端有对应应用处理消息来启动目标APP,不符合需求。
当前手机与手表已通过蓝牙连接,尝试使用RemoteActivityHelper从手机启动WearOS应用,但目标应用未启动。相关代码如下:
手机端按钮点击事件代码
RemoteActivityHelper remoteActivityHelper = new RemoteActivityHelper(this, Executors.newSingleThreadExecutor()); Uri uri1 = Uri.parse("eatmon2://mainactivity"); Intent intentAndroid = new Intent(Intent.ACTION_VIEW) .addCategory(Intent.CATEGORY_BROWSABLE) .setData(uri1); remoteActivityHelper.startRemoteActivity(intentAndroid, uri1.getHost());
WearOS应用AndroidManifest.xml代码
<activity android:name=".MainActivity" android:label="@string/app_name" android:exported="true"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="eatmon2" android:host="mainactivity" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
请问是否能实现该需求?有没有可行方法?
可行方案与问题修复
1. 确认RemoteActivityHelper的使用条件
RemoteActivityHelper确实支持从手机启动WearOS上的应用,无需额外的WearOS端消息处理应用,但需满足以下关键条件:
- 手机端APP必须是WearOS应用的配套手机应用(即通过
wearApp关联,在手机端build.gradle中配置wearApp project(':wear')或对应依赖) - 手机与手表处于已配对且连接正常状态(蓝牙或Wi-Fi连接均可)
- WearOS端目标Activity已正确配置intent-filter,且
android:exported="true"
2. 修复现有代码问题
你的代码存在两个核心问题导致启动失败:
- Intent缺少
DEFAULT类别:WearOS端intent-filter包含DEFAULT类别,但手机端构造的Intent仅添加了BROWSABLE,需补充Intent.CATEGORY_DEFAULT startRemoteActivity的目标节点参数错误:第二个参数应为WearOS设备的节点ID,而非URI的host。需通过NodeClient获取已连接的WearOS节点ID后传入
修复后的手机端代码
// 获取已连接的WearOS节点 NodeClient nodeClient = Wearable.getNodeClient(this); nodeClient.getConnectedNodes().addOnSuccessListener(nodes -> { if (!nodes.isEmpty()) { // 取第一个已连接节点(通常为配对的手表) String targetNodeId = nodes.get(0).getId(); RemoteActivityHelper remoteActivityHelper = new RemoteActivityHelper(this, Executors.newSingleThreadExecutor()); Uri uri1 = Uri.parse("eatmon2://mainactivity"); Intent intentAndroid = new Intent(Intent.ACTION_VIEW) .addCategory(Intent.CATEGORY_DEFAULT) .addCategory(Intent.CATEGORY_BROWSABLE) .setData(uri1); remoteActivityHelper.startRemoteActivity(intentAndroid, targetNodeId) .addOnSuccessListener(aVoid -> { // 启动请求发送成功 }) .addOnFailureListener(e -> { // 启动请求失败,打印错误信息排查 e.printStackTrace(); }); } });
3. 额外验证项
- 确认WearOS应用已正确安装到手表,且
MainActivity的intent-filter配置无误 - 手机端APP需申请对应权限:Android 12及以下需
BLUETOOTH、BLUETOOTH_ADMIN;Android 13+需BLUETOOTH_CONNECT,并在运行时获取权限 - 测试时确保手表处于解锁状态,部分WearOS设备锁定状态下无法启动第三方应用
4. 替代方案:指定包名与Activity启动
如果RemoteActivityHelper仍有问题,可直接构造目标应用的显式Intent,通过Wearable的MessageClient传递(无需WearOS端额外应用,但需知道目标应用的包名和Activity类名,且手机与WearOS应用签名一致):
// 获取已连接的WearOS节点 NodeClient nodeClient = Wearable.getNodeClient(this); nodeClient.getConnectedNodes().addOnSuccessListener(nodes -> { if (!nodes.isEmpty()) { String targetNodeId = nodes.get(0).getId(); Intent launchIntent = new Intent(Intent.ACTION_MAIN) .setComponent(new ComponentName("com.your.wear.package", "com.your.wear.package.MainActivity")) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // 将Intent转换为字节数组传递 byte[] intentBytes = RemoteIntentHelper.encodeIntent(launchIntent); Wearable.getMessageClient(this).sendMessage(targetNodeId, "/launch-app", intentBytes); } });
内容的提问来源于stack exchange,提问作者Rangaraju Lakshmi Gayathri
相关产品推荐
相关产品推荐

