android 通知的创建
来源:互联网 发布:js判断数字大于等于0 编辑:程序博客网 时间:2024/05/16 04:49
- 创建通知的builder
- 定义通知的action
- 设置通知点击的事件
- 发出通知
推荐你阅读:
- Notifications API Guide
- Intents and Intent Filters
- Notifications Design Guide
- 创建通知的builder
- 定义通知的action
- 设置通知点击的事件
- 发出通知
推荐你阅读:
- Notifications API Guide
- Intents and Intent Filters
- Notifications Design Guide
注:使用NotificationCompat.Builder
类需要因引入support包
Create a Notification Builder
在创建一个Notification时 NotificationCompat.Builder
用于定义其ui 。Builder
对象至少包含以下几个方法:
-
setSmallIcon()设置图标
-
setContentTitle()设置标题
setContentText() 设置详情文本
例如:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!");
定义Notification的行为
尽管action是可选的,建议为你的Notification添加一个。action 可以让你直接从通知栏转跳到你应用的activity中,在其中可以查看引发notification的事件和做相应的操作。在notification内部,action通过一个包含 Intent
的PendingIntent启动一个activity
。
如何构建 PendingIntent
取决于你启动 Activity
的类型。当你启动Activity
时候,你要遵循用户使用notification的经验。 在下面的例子中,点击通知打开了一个有效扩展notification的新的activity.,因此没有必要创建一个人工回退堆栈。 (从 在启动Activity的时候保存通知 获取更多信息):
Intent resultIntent = new Intent(this, ResultActivity.class);...// Because clicking the notification opens a new ("special") activity, there's// no need to create an artificial back stack.PendingIntent resultPendingIntent = PendingIntent.getActivity( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
设置notification的点击行为
用一个手势和之前的创建的 PendingIntent
关联,调用NotificationCompat.Builder适当的方法
。例如通过 setContentIntent()添加
PendingIntent,可以在
用户点击通的文本的时候启动一个activity。 如下:
PendingIntent resultPendingIntent;...mBuilder.setContentIntent(resultPendingIntent);
发出通知
发出通知要满足以下条件:
- 获取
NotificationManager
.的实例 - 使用
notify()
方法发出通知. 当调用notify()
, 方法添加特定的id.你可以通过这个id来更新notification.在Managing Notifications有更详细的描述. - 调用
build()
, 返回 包含说明Notification
的对象.For example:
NotificationCompat.Builder mBuilder;...// Sets an ID for the notificationint mNotificationId = 001;// Gets an instance of the NotificationManager serviceNotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);// Builds the notification and issues it.mNotifyMgr.notify(mNotificationId, mBuilder.build());
完整代码如下:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(this, AActicity.class);
// Because clicking the notification opens a new ("special") activity,
// there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
Notification notification = mBuilder.build();
//notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR; //该行代码可以使通知常驻通知栏
mNotifyMgr.notify(mNotificationId, notification);
- android 通知的创建
- Android创建使用默认布局的通知
- Android Notification通知栏的创建
- Android创建通知栏
- Android创建并更新通知栏通知
- android 创建通知栏Notification
- Android 简单创建一条通知
- 简单创建Android状态栏通知
- Android Wear创建通知的几种方式
- Android 状态栏通知Notification的创建与使用
- 创建通知-通知的用法
- android通知的使用
- 【Android】通知的实现
- Android 通知的使用
- Android通知的实现
- 简单的Android通知
- 【notification】Android 中创建震动通知
- 【notification】Android 中创建状态栏通知
- openstack网络模式之vlan分析
- 读懂C/C++递归
- 由于使用了nginx无法获取用户的真实IP
- 修改JAX-WS的序列化行为以及web service中传递Map对象
- java的反射机制浅谈
- android 通知的创建
- ORA-07445: exception encountered: core dump [qervwFetch()+338]
- AFK解析Json的使用-3-使用数据
- ruby
- 使用Node.js完成的第一个项目的实践总结
- 解决oracle11g 空表不能exp导出的问题
- linux 用命令设置系统音量大小
- base64加密、解密
- Android中的viewStub、requestFocus、include、merge标签(整理)