android Notification的使用

本文介绍了如何在Android中使用Notification,包括创建PendingIntent、设置Notification样式、添加自定义布局等步骤,并说明了如何清除Notification。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

手机的状态栏里出现的消息即是Notification,现在简单介绍下它的使用方法:

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

/**

创建一个PendingIntent,和Intent类似,不同的是由于不是马上调用,需要在下拉状态条出发的activity,所以采用的是PendingIntent,即点击Notification跳转启动到哪个Activity

*/
PendingIntent intent = PendingIntent.getActivity(context, 0, new Intent(context, MyPreferenceActivity.class), 0);

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.remoteview);//自定义notification的布局


Notification notification = new Notification.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("开始下载")
.setContentTitle("title")
.setNumber(1)

.setContent(remoteViews)
.setContentIntent(intent)
.build();

notification.flags = Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
manager.notify(NOTIFICATION_FLAG, notification); //为Notification添加flag,


当要手动清除该消息时调用:

manager.cancel(NOTIFICATION_FLAG); // 清除id为NOTIFICATION_FLAG的通知  




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值