随笔 - 632  文章 - 17  评论 - 54  阅读 - 93万

Android Wear创建一个通知

创建Android Wear的通知实际上和手机上创建没啥区别,主要是多了几个新类,只要用熟悉了一切都好办了。(如果只是测试通知,则直接运行wear app就能够看到效果)

创建一个简单的wear通知分为3步:

一、创建一个Intent用于设置你要做的动作

二、创建一个PendingIntent把Intent放进去(主要是根据intent传入的内容做跳转动作)

三、创建一个NotificationCompat.Builder用于设置通知内容,例如:将PendingIntent传递进去用于action的点击跳转,设置通知内容、设置通知标题、设置通知图标等

四、创建一个NotificationManagerCompat实例并调用notify()方法将通知发送出去

 

下面是实例代码:

1.创建一个通知

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void showNotification(Context context) {
        int notificationId = 001;
        Intent intent = new Intent();
        intent.setClass(context, ViewNotificationActivity.class);
        //传递数值
        //viewIntent.putExtra(EXTRA_EVENT_ID, eventId);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.ic_launcher)//设置notification小图标
                .setContentTitle("这是一个标题")//设置notification标题
                .setContentText("这是内容")//设置notification文本内容
                .setContentIntent(pendingIntent);//设置点击设置点击action时要跳转的页面(wear设备向左滑动第二个按钮的点击后所做的操作)
 
        //创建一个notifaicationmanamgercompat实例
        NotificationManagerCompat manager = NotificationManagerCompat.from(context);
        //将通知发送出去
        manager.notify(notificationId, builder.build());
    }

2.将上述通知在wear app项目中的主方法中调用运行即可

posted on   飘杨......  阅读(689)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2014-01-07 Android 使用第三方登录(QQ和新浪微博)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示