Notification-状态栏上的通知

当程序并非出在运行状态的时候,能够调用Notification来显示通知。java

一、建立android

    Notification的建立主要涉及到三个类:NotificationManager,Notification和PendingIntentui

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

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

    PendingIntent类能够理解成一个延迟执行的intent。code

     下面是一个实例代码:blog

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);

二、取消ip

    取消的方式有两种,分别为:get

    a、调用setAutoCancel方法,点击之后,Notification会自动消失it

    b、调用notificationManager.cancel方法,根据notificationId来取消Notification

实例代码以下:

/**
     * 取消方式一
     * 调用setAutoCancel方法,点击之后,Notification会自动消失
     * @param view
     */
    public void oneCancel(){
        notification =builder
                .setContentTitle("通知标题")
                .setContentText("通知内容")
                .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)//点击之后,Notification会自动消失
                .build();
        notificationManager.notify(NOTIFICATIONID,notification);
    }
    /**
     * 取消方式二
     * 调用notificationManager.cancel方法,根据notificationId来取消Notification
     * @param view
     */
    public void twoCancel(){
        notificationManager.cancel(NOTIFICATIONID);//根据notificationId来取消Notification
    }

三、技巧

    3.1 Notificationt带声音

/**
     * notificationt带声音
     * @param view
     */
    public void sound(){
        notification =builder
                .setContentTitle("notificationt带声音")
                .setContentText("notificationt带声音")
                .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luma.ogg")))//设置Notification建立的时候的提示音
                .setContentIntent(pendingIntent)
                .build();
        notificationManager.notify(NOTIFICATIONID,notification);
    }

    3.2 Notificationt带震动

/**
     * notificationt带震动
     * @param view
     */
    public void vibrate(View view){
        notification =builder
                .setContentTitle("notificationt带震动")
                .setContentText("notificationt带震动")
                .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentIntent(pendingIntent)
                //设置震动的频率,小标为0的静止的时长,下标为1的表示振动的时长,小标为2的静止的时长,以此类推
                .setVibrate(new long[]{0,1000,1000,1000})
                .build();
        notificationManager.notify(NOTIFICATIONID,notification);
    }

    3.3 Notificationt带LED灯光

/**
     * Notificationt带LED灯光
     * @param view
     */
    public void led(View view){
        notification =builder
                .setContentTitle("Notificationt带LED灯光")
                .setContentText("Notificationt带LED灯光")
                .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentIntent(pendingIntent)
                //LED灯光颜色,LED灯光亮起时长,LED灯光暗去时长
                .setLights(Color.GREEN,1000,1000)
                .build();
        notificationManager.notify(NOTIFICATIONID,notification);
    }

    3.4 Notificationt带bigText

/**
     * Notificationt带bigText
     * @param view
     */
    public void bigText(View view){
        notification =builder
                .setContentTitle("Notificationt带bigText")
                .setStyle(new NotificationCompat.BigTextStyle().bigText("Notificationt带bigTextNotificationt带bigTextNotificationt带" +
                        "bigTextNotificationt带bigTextNotificationt带bigTextNotificationt带"+
                        "bigTextNotificationt带bigTextNotificationt带bigTextNotificationt带bigT" +
                        "extNotificationt带bigTextNotificationt带bigTextNotificationt带bigText" +
                        "Notificationt带bigTextNotificationt带bigText"))
                .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentIntent(pendingIntent)
                .build();
        notificationManager.notify(NOTIFICATIONID,notification);
    }

    效果图为:

注意:这个是模拟器的效果,对于部分真机,则直接不显示文字内容

    3.5 Notificationt带bigPicture

notification =builder
                .setContentTitle("Notificationt带bigPicture")
                .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher_round)))
                .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentIntent(pendingIntent)
                .build();
        notificationManager.notify(NOTIFICATIONID,notification);

    注意:这个对模拟器有效果,对于部分真机,则直接不显示文字内容

3.6 Notificationt带priority

/**
     * Notificationt带priority
     * @param view
     */
    public void priority(View view){
        notification =builder
                .setContentTitle("Notificationt带priority")
                .setContentText("Notificationt带priority")
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentIntent(pendingIntent)
                .build();
        notificationManager.notify(NOTIFICATIONID,notification);
    }

    优先级的参数从低到高分别为:PRIORITY_DEFAULT表示默认的重要程度,和不设置效果是同样的。PRIORITY_MIN表示最低的重要程度,系统只会在特定的场景中才会显示这条通知,好比用户下拉状态栏的时候;PRIORITY_LOW表示较低的重要程度,系统可能会将这类通知缩小,或者是改变其显示的顺序(有一部分手机会修改显示顺序),并将其排在更重要的通知以后;PRIORITY_HIGH表示较重的重要程度,系统可能会将这类通知放大,或者是改变其显示顺序,排在比较靠前的位置;PRIORITY_MAX表示最重要的通知,有一些手机上面会直接显示成一个弹窗的形式,有一些仍是改变显示顺序。

四、自定义部分

      参考地址:https://developer.android.com/training/notify-user/custom-notification#java

相关文章
相关标签/搜索