为什么我不能从 Android Wear 上的通知页面启动 Activity? [英] Why can't I start an Activity from a page Notification on Android Wear?

查看:31
本文介绍了为什么我不能从 Android Wear 上的通知页面启动 Activity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I just succesfully created a bunch of pages notifications on my Wear device.

唯一的问题是PendingIntent好像没有启动Activity(当然是在 Manifest 中声明的).

The only problem is that the PendingIntent does not seems to start an Activity (which is of course declared in Manifest).

这是我的代码:

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

当我按下通知时,我希望意图开始,但没有任何反应..

When I press on a Notification, I expect the intent to start, but nothing happens..

欢迎提出任何建议.

此代码在通知之后有效,因此,可以轻松启动第二个活动,而不会出现错误:

This code works, just after the notification, so, the second activity can easily be launched withour bug:

 startActivity(viewIntent);

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

There is now an "open" button at the end that works fine, but still nothing happens on individual notifications (every pages)

推荐答案

  1. 页面不可点击 - 在 Android Wear 上,只有操作可点击.对于手机生成的通知,它们只会出现在所有页面之后
  2. 如果您对手机生成的通知有内容意图,该通知将始终显示为在手机上打开"操作.除非您移除内容 Intent(使通知无法在手机上点击),否则无法禁用此功能.

我说手机生成",因为您也可以创建 Wear 应用.通过使用数据层将消息推送到您的 Wear 应用,然后 Wear 应用可以构建自定义通知.这些通知允许您使用 setDisplayIntent() 并内联显示活动(作为主页面或作为单独的页面).当然,这些活动可以包含您想要的任何视图,包括执行任何操作的操作(例如将消息发送回手机以启动特定活动).

I say 'phone generated' as you can also 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() and display activities inline (either as the main page or as separate pages). These activities can, of course, contain any View you want, including actions to perform any action (such as send a message back to the phone to start a particular activity).

请注意,由于默认情况下页面不可点击,自定义通知的样式应使项目可点击非常明显.您可以考虑使用 ,而不是使用自定义通知活动setContentAction() 显示与布局其余部分内嵌的操作图标 - 这会将操作作为一个单独的元素移除,并将其直接放置在通知/页面上.

Note that because pages are not clickable by default, styling of a custom notification should make it very obvious that the items are clickable. Rather than using a custom notification activity, you may consider using setContentAction() to display the action icon inline with the rest of the layout - this removes the action as a separate element past the action and places it directly on the notification/page.

这篇关于为什么我不能从 Android Wear 上的通知页面启动 Activity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆