Notification-状态栏上的通知

本文介绍了Android中创建和管理Notification的方法,包括使用NotificationManager、Notification和PendingIntent。讲解了如何设置PendingIntent的标志,以及如何取消Notification。还分享了Notification的高级特性,如带声音、震动、LED灯光、bigText和bigPicture,并讨论了不同优先级的效果。最后提到了自定义Notification的资源和遇到的问题链接。
摘要由CSDN通过智能技术生成

当程序并不是出在运行状态的时候,可以调用Notification来显示通知。

1、创建

    Notification的创建主要涉及到三个类:NotificationManager,Notification和PendingIntent

    NotificationManager主要是对通知进行管理。

    Notification类主要用于对Notification的一些属性进行定义。获得的方式主要是通过兼容各个版本的support-v4中的NotificationCompat类来获得。

    PendingIntent类可以理解成一个延迟执行的intent。

     下面是一个实例代码:

public static final int NOTIFICATIONID = 0x101;    
private NotificationManager notificationManager;
private NotificationCompat.Builder builder;
private Notification notification;
private PendingIntent pendingIntent;
notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
builder = new NotificationCompat.Builder(NotificationManiActivity.this,null);
Intent intent = new Intent(NotificationManiActivity.this,NotificationTestActivity.class);
pendingIntent = PendingIntent.getActivity(NotificationManiActivity.this,0,intent,0);
notification =builder
                .setContentTitle("通知标题")
                .setContentText("通知内容")
                .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher)//Notification什么时候显示
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentIntent(pendingIntent)
                .build();
notificationManager.notify(NOTIFICATIONID,notification);

1.1 PendingIntent

        PendingIntent的获得,可以调用自己的静态方法getActivity()、getBroadcast()、或者是getService()。参数都是一样的。第一个参数是Context,第二个参数是requestCode,第三个参数是intent。第四个参数是int类型的flag,可以分为以下四个值:FLAG_ONE_SHOT,FLAG_NO_CREATE,FLAG_CANCEL_CURRENT,FLAG_UPDATE_CURRENT.分别解释为:

FLAG_ONE_SHOT:Flag indicating that this PendingIntent can be used only once表示此PendingIntent只能够使用一次。

FLAG_NO_CREATE:Flag indicating that if the described PendingIntent does not already exist, then simply return null instead of creating it. 定义的PendingIntent如果不存在,直接返回null,不去进行创建

FLAG_CANCEL

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值