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项目中的主方法中调用运行即可
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
2014-01-07 Android 使用第三方登录(QQ和新浪微博)