Android Wear - start wear activity from handheld a

2019-02-17 21:43发布

I'm sending a notification from an android handheld device to an android wear device. I'd like to include an action with the notification that starts an activity on the android wear device.

How do I set the pending intent to an activity on the wear device? I'm able to launch the activity if the notification is created on the wearable, but not if the notification is created on the phone and then sent to the wearable.

 // 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 manifest from wear app that I'm trying to launch:

    <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>

2条回答
迷人小祖宗
2楼-- · 2019-02-17 21:55

Have a look at the DataLayer sample included in the Android SDK Manager for API 20. It contains an example of how to start up an activity on the wearable after a user presses a button on the mobile device.

查看更多
Juvenile、少年°
3楼-- · 2019-02-17 22:00

that probably is done with a bit of hackery.

create your own action and make the Wear.activity <intent-filter to use it. Something like:

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

then on your notification intent, created from the phone, you know it, just use that one:

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

and voila !

查看更多
登录 后发表回答