Android Wear - 如何从手持设备启动穿戴设备应用活动

5
我正在从一部安卓手持设备向另一部安卓智能手表发送通知。我想在通知中包含一个操作,该操作可在安卓智能手表上启动一个活动。
如何将待定意图设置为智能手表上的活动?如果通知是在可穿戴设备上创建的,则我可以启动活动,但如果是在手机上创建通知,然后发送到可穿戴设备上,则无法成功启动活动。
 // creating action for push notification created on handheld
 public NotificationCompat.Action CreateWearAppAction(Integer metricId) {
    // I wasnt able to look up the wear's main activity class since this is in the handheld project.
    Intent wearPageIntent = new Intent(Intent.ACTION_VIEW);
    Uri wearUri = Uri.parse("mywearapp://Test?MetricId=" + metricId);
    wearPageIntent.setData(wearUri);
    PendingIntent wearPagePendingIntent = PendingIntent.getActivity(context, 0, wearPageIntent, 0);

    return new NotificationCompat.Action.Builder(R.drawable.ic_action_metric,
            "Open Charts on Wear", wearPagePendingIntent)
            .build();
}

我正在尝试启动的穿戴应用程序的活动清单:

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:noHistory="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <!-- deep link -->
        <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="mywearapp" />
        </intent-filter>
    </activity>

你能添加一些代码吗?需要 Wear service、Wear manifest 和移动电话调用。 - Heinrisch
2个回答

5
请查看Android SDK Manager中API 20附带的DataLayer示例。它包含了一个示例,演示了如何在用户按下移动设备上的按钮后,在可穿戴设备上启动一个活动。

仅提供一些细节,基本上这是关于通过手持设备向可穿戴设备发送消息的 Wearable.MessageApi.sendMessage。 - mdiener

0

这可能是通过一些技巧完成的。

创建自己的操作并使 Wear.activity <intent-filter 使用它。类似于:

    <intent-filter>
        <action android:name="com.myCompany.myApp.ACTION.openWearActivity" />
    </intent-filter>

然后在您从手机创建的通知意图中,您知道它,只需使用那个:

new Intent("com.myCompany.myApp.ACTION.openWearActivity");

完成了!


我尝试了这个,但仍然无法启动可穿戴设备活动。 - TWilly
你可能需要添加 <category android:name="android.intent.category.DEFAULT" /> - Matthew
也尝试了添加类别,但仍无法启动我的穿戴活动。 - Karim Varela

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接