android 通知的创建
- 创建通知的builder
- 定义通知的action
- 设置通知点击的事件
- 发出通知
推荐你阅读:
- Notifications API Guide
- Intents and Intent Filters
- Notifications Design Guide
注:使用NotificationCompat.Builder
类需要因引入support包
推荐你阅读:
- 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);
>更多相关文章
首页推荐
佛山市东联科技有限公司一直秉承“一切以用户价值为依归
- 01-11全球最受赞誉公司揭晓:苹果连续九年第一
- 12-09罗伯特·莫里斯:让黑客真正变黑
- 12-09谁闯入了中国网络?揭秘美国绝密黑客小组TA
- 12-09警示:iOS6 惊现“闪退”BUG
- 05-15奇安信:数据安全问题是医疗卫生行业数字化
- 05-15腾讯宣布混元文生图大模型开源
- 05-15网易云音乐与韩国知名娱乐公司Kakao达成战略
- 05-15阿联酋宣告正式开启“5G-A全国商用”计划,
- 05-15二十年未遇的强烈地磁暴来袭
相关文章
24小时热门资讯
24小时回复排行
热门推荐
最新资讯
操作系统
黑客防御