Android中Notification的使用

本文介绍如何使用Notification在通知栏发送消息提醒,包括设置图标、标题、内容等,并通过Intent实现点击后的跳转。

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

Notification在通知栏发送通知以进行消息提醒
private int NOTIFICATION_ID = 0;//设置Notification的ID以便后续更新消息内容

    Intent intent = new Intent(getApplicationContext(), MainActivity.class);//设置一个Intent进行点击后的操作
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);//设置点击后跳转到Intent

    Builder builder = new Builder(MainActivity.this);//此处需要引用android.support.v7.app.NotificationCompat.Builder
        builder.setSmallIcon(R.drawable.ic_luancher);//设置Notification图标
        builder.setContentTitle("新消息!");//设置Notification的标题
        builder.setContentText("您有新消息!");//设置Notification的内容
    builder.setSubText("消息的详细内容!");//设置Notification的扩展信息
        builder.setAutoCancel(true);//设置Notification可以清除
    builder.setContentIntent(pendingIntent);//设置点击后跳转到Intent
        Notification notification = builder.build();//创建并获取Notification的真实对象
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//获取服务
        notificationManager.notify(NOTIFICATION_ID,notification);//ID用于后续更新Notification内容
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值