Android Wear - start wear activity from handheld action

家住魔仙堡 提交于 2019-12-18 06:37:25

问题


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>

回答1:


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.




回答2:


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 !



来源:https://stackoverflow.com/questions/25167859/android-wear-start-wear-activity-from-handheld-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!