编程环境android4.4+eclipse
定义
NotificationManager manager;
NotificationCompat.Builder notifyBuilder;
在onCreate中
/* 实例化NotificationManager以获取系统服务 */
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
使用
notifyBuilder = new NotificationCompat.Builder(this)
/* 设置small icon */
.setSmallIcon(R.drawable.ic_launcher)
/* 设置title */
.setContentTitle("打卡")
/* 设置详细文本 */
.setContentText(item.title).setTicker("足迹消息")
/* 设置点击后通知消失 */
.setAutoCancel(true)
/* 意图 点击跳转到SignActivity */
.setContentIntent(pendingIntent);
abc_id++;
manager.notify(100 * abc_id, notifyBuilder.build());
setContentIntent(pendingIntent);
意图 可使用也可不使用,使用需定义下
// 点击的意图ACTION是跳转到Intent
Intent resultIntent = new Intent(this, SignActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
修改后
NotificationManager notificationManager;
NotificationCompat.Builder nb;
String svcName = Context.NOTIFICATION_SERVICE;
notificationManager = (NotificationManager) getSystemService(svcName);
nb = new Builder(this);
使用
nb2.setSmallIcon(R.drawable.ic_launcher).setContentTitle("标题")
.setWhen(System.currentTimeMillis())
.setContentText("内容内容内容"+ncc).setAutoCancel(true).setContentInfo("info");
ncc++;
notificationManager.notify(ncc, nb2.build());
解释:
状态栏消息
示例
**** 标题 21:12
**** 内容内容内容"+ncc info
****
星号是图片区域
setSmallIcon 设置图标
setContentTitle 设置示例中的标题部分
setContentText 设置示例中的内容部分
setContentInfo 设置示例中时间下面的info部分
setAutoCancel 设置消息是否可以被点击取消(被单击后自动取消自己)
setWhen 设置展开的状态栏按时间顺序排序通知
笔记 android发送状态栏消息
最新推荐文章于 2022-07-26 00:00:00 发布