Nofitication的使用

本文详细介绍如何使用Android系统创建Notification实例,包括创建NotificationBuilder、定义Notification行为、设置点击操作及启动和取消Notification等步骤。

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

Notification的例子,请参考附件。

1. 创建Notification builder

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon) // 一个图标
.setContentTitle("My notification") // 标题
.setContentText("Hello World!"); // 具体内容

2. 定义notification action

Intent resultIntent = new Intent(this, ResultActivity.class); // 点击后跳转的activity
...
// 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
);

3. 设置notification点击的行为

PendingIntent resultPendingIntent;
...
mBuilder.setContentIntent(resultPendingIntent);

4. 启动notification

NotificationCompat.Builder mBuilder;
...
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());

5. 取消notification

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

nm.cancel(NOTIFICATION_ID);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值