Android Wear创建一个通知
2016-01-07 10:53
525 查看
创建Android Wear的通知实际上和手机上创建没啥区别,主要是多了几个新类,只要用熟悉了一切都好办了。(如果只是测试通知,则直接运行wear app就能够看到效果)
创建一个简单的wear通知分为3步:
一、创建一个Intent用于设置你要做的动作
二、创建一个PendingIntent把Intent放进去(主要是根据intent传入的内容做跳转动作)
三、创建一个NotificationCompat.Builder用于设置通知内容,例如:将PendingIntent传递进去用于action的点击跳转,设置通知内容、设置通知标题、设置通知图标等
四、创建一个NotificationManagerCompat实例并调用notify()方法将通知发送出去
下面是实例代码:
1.创建一个通知
2.将上述通知在wear app项目中的主方法中调用运行即可
创建一个简单的wear通知分为3步:
一、创建一个Intent用于设置你要做的动作
二、创建一个PendingIntent把Intent放进去(主要是根据intent传入的内容做跳转动作)
三、创建一个NotificationCompat.Builder用于设置通知内容,例如:将PendingIntent传递进去用于action的点击跳转,设置通知内容、设置通知标题、设置通知图标等
四、创建一个NotificationManagerCompat实例并调用notify()方法将通知发送出去
下面是实例代码:
1.创建一个通知
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项目中的主方法中调用运行即可
相关文章推荐
- Android 原生加载框
- 高德地图 android 设置完全自定义的InfoWindow
- Activity和Service的交互
- 【Android】实现XML解析的几种技术
- Android权限总结
- Android代码混淆
- listView分组管理
- Android之创建项目
- notifyDataSetChanged的一个坑
- Android实际开发问题03------ListView中添加Edittext
- Android获取View宽高的几种方式
- Android中Service的使用详解
- 把android的开发环境配置到Mac上
- Android设备抓包命令及分析
- Android Studio 下载SDK
- 10 条提升 Android 性能的建议
- android需要尽快掌握的
- android 半透明圆角dialog的简单实现
- Android中使用shape自定义纯色图片总结
- android studio 出现 Gradle sync failed:Plugin is too old, please update to a more recent v