为什么我无法从 Android Wear 上的页面通知启动 Activity?

Why can't I start an Activity from a page Notification on Android Wear?

我刚刚在我的 Wear 设备上成功创建了一堆页面通知。

唯一的问题是 PendingIntent 似乎没有启动 Activity (这当然是在清单中声明的​​)。

这是我的代码:

List extras = new ArrayList();
Intent viewIntent = new Intent(getApplicationContext(), DetailActivity.class);
viewIntent.putExtra("KEY", "TEST123");
//Note: I also tried: Intent viewIntent = new Intent(getApplicationContext(), DetailActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent viewPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, viewIntent, 0);

for (Route aRoute : myRoutes) {
    Notification aNotif = new NotificationCompat.Builder(getApplicationContext())
    .setContentTitle("BUS " + aRoute.route_short_name)
    .setContentText(aRoute.directions.get(0).trip_headsign)
    .setLargeIcon(bitmap)
    .setContentIntent(viewPendingIntent)
    .setSmallIcon(R.mipmap.ic_launcher).build();

    extras.add(aNotif);
}

NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this)
    .setContentTitle(title)
    .setContentText(desc)
    .setContentIntent(viewPendingIntent)//Just in case
    .setSmallIcon(R.mipmap.ic_launcher);

Notification notification = builder1
    .extend(new NotificationCompat.WearableExtender()
    .addPages(extras))
    .setContentIntent(viewPendingIntent)//Just in case
    .build();

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(0, notification);

当我按下通知时,我希望 intent 启动,但什么也没发生..

欢迎提出任何建议。

编辑: 这段代码在通知之后有效,因此,第二个 activity 可以很容易地通过我们的错误启动:

 startActivity(viewIntent);

编辑2: 现在末尾有一个 "open" 按钮可以正常工作,但个别通知(每页)仍然没有任何反应

  1. 页面不可点击 - 在 Android Wear 上,只有操作可点击。对于 phone 生成的通知,这些通知只出现在所有页面
  2. 之后
  3. 如果您在 phone 生成的通知中有内容意图,那将始终显示为 'Open on phone' 操作。除非您删除您的内容意图(使通知在 phones 上不可点击),否则无法禁用此功能。

我说 'phone generated' 因为您也可以 create a Wear app. By using the data layer to push messages to your Wear app, the Wear app can then build custom notifications. These notifications allow you to use setDisplayIntent() 并显示内联活动(作为主页或单独的页面)。当然,这些活动可以包含您想要的任何视图,包括执行任何操作的操作(例如将消息发回 phone 以启动特定的 activity)。

请注意,由于默认情况下页面不可点击,因此自定义通知的样式应使这些项目可点击非常明显。与其使用自定义通知 activity,不如考虑使用 setContentAction() 来显示与布局的其余部分内联的操作图标 - 这会将操作作为单独的元素移除,并直接放置在notification/page.