Android Wear 通知
让我们来讨论 Android Wear 通知,并通过示例学习如何构建和显示可穿戴设备上的通知
引言
在本文中,我将与您讨论如何在 Android Wear 中构建通知。通知在大多数可穿戴设备应用程序中都很常见。作为开发者,必须了解如何构建通知。
构建通知使用以下类组合:
- 通知
- NotificationCompact.Action
- NotificationCompat.Builder
- NotificationCompat.WearableExtender
- NotificationManagerCompat
如果您是 Android 程序员,您应该已经知道如何在应用程序中创建和使用通知。
可穿戴设备中的通知设计方式相似,但有一个称为堆叠通知的特殊功能,仅在可穿戴设备上可见。堆叠通知只不过是一组堆叠在一起的通知,显示为一张卡片。它允许用户选择和阅读所有通知。
以下是您可以学习如何构建通知的各种示例链接:
背景
请查看以下链接以了解 Android Wear。
https://codeproject.org.cn/Articles/1038337/Introduction-to-Android-Wear
使用代码
示例 1 – 构建 Notification 实例
下面是构建简单 Notification
实例的代码示例。我们还没有显示通知,这将在下一步学习。
Notification notification = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("CP notification") .setContentText("CodeProject wear notification!") .build();
示例 2 – 扩展 Notification
在此示例中,我们将通过扩展通知实例来添加可穿戴设备功能,以包含 WearableExtender
实例。
您可以根据需要设置 WearableExtender
的任何可用属性。最后,必须通过调用“extend”方法并将 WearableExtender
实例作为参数传递,将其附加到 Notification
实例。在此示例中,您还可以看到下面正在使用 NotificationManagerCompat
实例来创建可穿戴通知。
引用:代码信息请注意 – 以下代码片段是开源示例的一部分 - https://github.com/LarkspurCA/WearableSuggest
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender() .setHintShowBackgroundOnly(true); Notification notification = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Hello Android Wear") .setContentText("First Wearable notification.") .extend(wearableExtender) .build(); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); int notificationId = 1; notificationManager.notify(notificationId, notification);
示例 3 – 堆叠通知
在此示例中,我们将学习如何构建堆叠通知。下面是显示堆叠通知的代码片段。
您可以看到下面有两个或更多 Notification
实例,并且使用 NotificationManagerCompat
实例来显示分组通知。为了使堆叠通知一起显示,我们必须对通知进行分组,这可以通过为所有您感兴趣的 Notification
实例设置相同的组键来完成。
public void DisplayStackedNotifications() { final String GROUP_KEY = "group_messages"; Notification notification1 = new NotificationCompat.Builder(this) .setContentTitle("Message from User1") .setContentText("What's for lunch? " + "Can we have a veggie burger?") .setSmallIcon(R.mipmap.ic_launcher) .setGroup(GROUP_KEY) .build(); Notification notification2 = new NotificationCompat.Builder(this) .setContentTitle("Message from CodeProject") .setContentText("How is your article writing going?") .setSmallIcon(R.mipmap.ic_launcher) .setGroup(GROUP_KEY) .build(); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(1, notification1); notificationManager.notify(2, notification2); }
示例 4 – 向通知添加操作
在此示例中,让我们看看如何向通知添加操作。有时,您不仅希望显示通知,还希望包含一些要触发的操作,例如打开新活动或执行其他有趣的操作,那么您应该添加操作。
在可穿戴设备上有一种叫做主要操作的东西,您可以通过调用 setContentIntent
在 NotificationCompat.Builder
上设置内容操作。
引用:代码信息请注意 – 以下代码片段基于 http://stackoverflow.com/questions/6391870/how-exactly-to-use-notification-builder
public void NotificationWithPrimaryIntent() { int notificationID = 0; NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("Notification") .setContentText("Test notification with Content Intent"); Intent notificationIntent = new Intent(this, MyDisplayActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(contentIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(notificationID, builder.build()); }
让我们看看如何向通知添加自定义操作。下面是代码片段。我们做了以下操作:
1) 我们正在创建一个带有 ACTION_DIAL
操作的 Intent 实例,用于拨打已连接的可穿戴设备的电话。
2) 创建 PendingIntent
实例以使用拨打电话的 Intent。接下来,我们使用图标、文本和待定意图构建 NotificationCompat.Action
。
3) 使用图标、文本、标题创建 NotificationCompat.Builder
实例。您还会注意到,通过调用 addAction 并传入 NotificationCompat.Action
实例,正在添加一个操作。
4) 最后,创建 NotificationManager
实例,通过调用“notify”方法并传入通知 ID 和 NotificationCompat.Builder
实例来通知用户。
public void NotificationWithAction() { int notificationID = 0; Intent callPhoneIntent = new Intent(Intent.ACTION_DIAL); PendingIntent callPhonePendingIntent = PendingIntent.getActivity(this, 0, callPhoneIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Action callPhoneAction = new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "Call Phone", callPhonePendingIntent) .build(); NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("Notification") .setContentText("Call Phone") .addAction(callPhoneAction); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(notificationID, builder.build()); }
示例 5 – 通过广播接收器发送通知
在此示例中,我们将尝试了解如何在广播接收器中显示通知。我们需要一个 Intent
实例来触发或发送广播。此外,BroadcastReceiver
负责接收广播消息。
我们需要修改 AndroidManifest.xml
文件,以包含具有意图过滤器操作 com.example.ranjan.wearablenotification.SHOW_NOTIFICATION
的广播接收器。添加此更改非常重要,否则广播接收器将无法接收任何内容。
<receiver android:name=".MyPostNotificationReceiver" android:exported="true" > <intent-filter> <action android:name="com.example.ranjan.wearablenotification.SHOW_NOTIFICATION" /> </intent-filter> </receiver>
下面是广播接收器的代码片段。我们重写了 onReceive
方法并实现了通知的代码。您可以在下面看到使用图标、标题等创建了一个 Notification
实例。然后,我们使用 NotificationManager
实例来通知它。
public class MyPostNotificationReceiver extends BroadcastReceiver { public static final String CONTENT_KEY = "contentText"; public MyPostNotificationReceiver() { } @Override public void onReceive(Context context, Intent intent) { Intent displayIntent = new Intent(context, MyDisplayActivity.class); String text = intent.getStringExtra(CONTENT_KEY); Notification notification = new Notification.Builder(context) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(text) .extend(new Notification.WearableExtender() .setDisplayIntent(PendingIntent.getActivity(context, 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT))) .build(); ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)) .notify(0, notification); Toast.makeText(context, context.getString(R.string.notification_posted), Toast.LENGTH_SHORT).show(); } }
下面是发送广播消息的代码片段。我们创建了一个 Intent
实例,并设置了一个与 intent-filter
中设置的相同的操作。通过调用 putExtra
并使用 CONTENT_KEY
作为键和 R.string.title
作为值来设置通知文本。
public class MyStubBroadcastActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent intent = new Intent(); intent.setAction("com.example.ranjan.wearablenotification.SHOW_NOTIFICATION"); intent.putExtra(MyPostNotificationReceiver.CONTENT_KEY, getString(R.string.title)); sendBroadcast(intent); finish(); } }
关注点
我总是在学习新东西。在 Android Wear 中,通知被高度重视,学习并充分利用它们非常重要。
历史
版本 1.0 - 发布了文章的初始版本,并附带了一个代码示例 - 2015 年 10 月 16 日