• Android -- 再来一发Notification


    之前写过一个Notificaiton的文章,用上面的方式去操作也是OK的,但是到后面的SDK之后,有些方法被弃用,甚至我到SDK23的时候,我发现有些方法直接没了,所以在这里重新写一下最新的用法。
    http://www.cnblogs.com/yydcdut/p/3828941.html

    步骤

    • 创建一个通知栏的Builder构造类
    • 定义通知栏的Action
    • 设置通知栏点击事件
    • 通知

    代码

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
    
    otificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); 
    
    mBuilder.setContentTitle("测试标题")//设置通知栏标题  
        .setContentText("测试内容") //设置通知栏显示内容</span>  
        .setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL)) //设置通知栏点击意图  
    //  .setNumber(number) //设置通知集合的数量  
        .setTicker("测试通知来啦") //通知首次出现在通知栏,带上升动画效果的  
        .setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间  
        .setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级  
    //  .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消    
        .setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)  
        .setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合  
        //Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission  
        .setSmallIcon(R.drawable.ic_launcher);
    

    对应的各个方法的属性

    Flags

    功能:提醒标志符,向通知添加声音、闪灯和振动效果等设置达到通知提醒效果,可以组合多个属性

    有2种设置方法:

    1. 实例化通知栏之后通过给他添加.flags属性赋值。

      Notification notification = mBuilder.build();  
      notification.flags = Notification.FLAG_AUTO_CANCEL;  
      
    2. 通过setContentIntent(PendingIntent intent)方法中的意图设置对应的flags

    public PendingIntent getDefalutIntent(int flags){  
        PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, new Intent(), flags);  
        return pendingIntent;  
    }  
    

    提醒标志符成员

    Notification.FLAG_SHOW_LIGHTS              //三色灯提醒,在使用三色灯提醒时候必须加该标志符
    Notification.FLAG_ONGOING_EVENT          //发起正在运行事件(活动中)
    Notification.FLAG_INSISTENT   //让声音、振动无限循环,直到用户响应 (取消或者打开)
    Notification.FLAG_ONLY_ALERT_ONCE  //发起Notification后,铃声和震动均只执行一次
    Notification.FLAG_AUTO_CANCEL      //用户单击通知后自动消失
    Notification.FLAG_NO_CLEAR          //只有全部清除时,Notification才会清除 ,不清楚该通知(QQ的通知无法清除,就是用的这个)
    Notification.FLAG_FOREGROUND_SERVICE    //表示正在运行的服务
    
    

    setDefaults(int defaults)

    NotificationCompat.Builder中的方法,用于提示。

    功能:向通知添加声音、闪灯和振动效果的最简单、使用默认(defaults)属性,可以组合多个属性(和方法1中提示效果一样的)

    Notification.DEFAULT_VIBRATE    //添加默认震动提醒  需要 VIBRATE permission
    Notification.DEFAULT_SOUND    // 添加默认声音提醒
    Notification.DEFAULT_LIGHTS// 添加默认三色灯提醒
    Notification.DEFAULT_ALL// 添加默认以上3种全部提醒
    

    setVibrate(long[] pattern)

    .setVibrate(new long[] {0,300,500,700}); 
    //或者
    mBuilder.build().vibrate = new long[] {0,300,500,700};  
    

    setLights(intledARGB ,intledOnMS ,intledOffMS )

    功能:android支持三色灯提醒,这个方法就是设置不同场景下的不同颜色的灯。

    描述:其中ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间。

    注意:1)只有在设置了标志符Flags为Notification.FLAG_SHOW_LIGHTS的时候,才支持三色灯提醒。2)这边的颜色跟设备有关,不是所有的颜色都可以,要看具体设备。

    .setLights(0xff0000ff, 300, 0)  
      
    Notification notify = mBuilder.build();  
    notify.flags = Notification.FLAG_SHOW_LIGHTS;  
    notify.ledARGB = 0xff0000ff;  
    notify.ledOnMS = 300;  
    notify.ledOffMS = 300; 
    

    setSound(Uri sound)

    //获取默认铃声  
    .setDefaults(Notification.DEFAULT_SOUND)  
    //获取自定义铃声  
    .setSound(Uri.parse("file:///sdcard/xx/xx.mp3"))  
    //获取Android多媒体库内的铃声  
    .setSound(Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "5")) 
    

    setOngoing(boolean ongoing)

    功能:设置为ture,表示它为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)

    setProgress(int max, int progress,boolean indeterminate)

    属性:max:进度条最大数值  、progress:当前进度、indeterminate:表示进度是否不确定,true为不确定,如下第3幅图所示  ,false为确定下第1幅图所示

    注意:此方法在4.0及以后版本才有用,如果为早期版本:需要自定义通知布局,其中包含ProgressBar视图

    使用:如果为确定的进度条:调用setProgress(max, progress, false)来设置通知,在更新进度的时候在此发起通知更新progress,并且在下载完成后要移除进度条,通过调用setProgress(0, 0, false)既可。

    如果为不确定(持续活动)的进度条,这是在处理进度无法准确获知时显示活动正在持续,所以调用setProgress(0, 0, true) ,操作结束时,调用setProgress(0, 0, false)并更新通知以移除指示条

    自定义View

    这里要用到RemoteViews这个类。

    Notification的自定义布局是RemoteViews,和其他RemoteViews一样,在自定义视图布局文件中,仅支持FrameLayout、LinearLayout、RelativeLayout三种布局控件和AnalogClock、Chronometer、Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView和AdapterViewFlipper这些显示控件,不支持这些类的子类或Android提供的其他控件。否则会引起ClassNotFoundException异常。

    public void showButtonNotify(){  
            NotificationCompat.Builder mBuilder = new Builder(this);  
            RemoteViews mRemoteViews = new RemoteViews(getPackageName(), R.layout.view_custom_button);  
            mRemoteViews.setImageViewResource(R.id.custom_song_icon, R.drawable.sing_icon);  
            //API3.0 以上的时候显示按钮,否则消失  
            mRemoteViews.setTextViewText(R.id.tv_custom_song_singer, "周杰伦");  
            mRemoteViews.setTextViewText(R.id.tv_custom_song_name, "七里香");  
            //如果版本号低于(3。0),那么不显示按钮  
            if(BaseTools.getSystemVersion() <= 9){  
                mRemoteViews.setViewVisibility(R.id.ll_custom_button, View.GONE);  
            }else{  
                mRemoteViews.setViewVisibility(R.id.ll_custom_button, View.VISIBLE);  
            }  
            //  
            if(isPlay){  
                mRemoteViews.setImageViewResource(R.id.btn_custom_play, R.drawable.btn_pause);  
            }else{  
                mRemoteViews.setImageViewResource(R.id.btn_custom_play, R.drawable.btn_play);  
            }  
            //点击的事件处理  
            Intent buttonIntent = new Intent(ACTION_BUTTON);  
            /* 上一首按钮 */  
            buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PREV_ID);  
            //这里加了广播,所及INTENT的必须用getBroadcast方法  
            PendingIntent intent_prev = PendingIntent.getBroadcast(this, 1, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);  
            mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_prev, intent_prev);  
            /* 播放/暂停  按钮 */  
            buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PALY_ID);  
            PendingIntent intent_paly = PendingIntent.getBroadcast(this, 2, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);  
            mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_play, intent_paly);  
            /* 下一首 按钮  */  
            buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_NEXT_ID);  
            PendingIntent intent_next = PendingIntent.getBroadcast(this, 3, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);  
            mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_next, intent_next);  
              
            mBuilder.setContent(mRemoteViews)  
                    .setContentIntent(getDefalutIntent(Notification.FLAG_ONGOING_EVENT))  
                    .setWhen(System.currentTimeMillis())// 通知产生的时间,会在通知信息里显示  
                    .setTicker("正在播放")  
                    .setPriority(Notification.PRIORITY_DEFAULT)// 设置该通知优先级  
                    .setOngoing(true)  
                    .setSmallIcon(R.drawable.sing_icon);  
            Notification notify = mBuilder.build();  
            notify.flags = Notification.FLAG_ONGOING_EVENT;  
            mNotificationManager.notify(notifyId, notify);  
        }  
    

    大视图风格通知

    注:4.1之前的版本不支持大视图

    详情区域根据用途可有多种风格:

    1. NotificationCompat.BigPictureStyle 大图片风格:详情区域包含一个256dp高度的位图
    2. NotificationCompat.BigTextStyle 大文字风格:显示一个大的文字块
    3. NotificationCompat.InboxStyle  收件箱风格:显示多行文字
    NotificationCompat.BigPictureStyle inboxStyle = new NotificationCompat.InboxStyle();  
            String[] events = new String[5];  
            // Sets a title for the Inbox style big view  
            inboxStyle.setBigContentTitle("大视图内容:");  
            // Moves events into the big view  
            for (int i=0; i < events.length; i++) {  
                inboxStyle.addLine(events[i]);  
            }  
            mBuilder.setContentTitle("测试标题")  
                    .setContentText("测试内容")  
    //              .setNumber(number)//显示数量  
                    .setStyle(inboxStyle)//设置风格  
                    .setTicker("测试通知来啦");  
    

    我是天王盖地虎的分割线

    http://adchs.github.io/patterns/notifications.html

    http://blog.csdn.net/vipzjyno1/article/details/25248021

  • 相关阅读:
    程序相关概念
    C/C++程序语言概念
    详解QT5.10.0搭载OpenCV3.4.0环境配置步骤说明
    微信小程序支付结果 c#后台回调
    信小程序支付(C#后台+前台)
    微信小程序知识集锦
    WPF获取读取电脑指定文件夹中的指定文件的地址
    WPF后台动画DoubleAnimation讲解
    wpf datagrid 的单元格内容超出列宽度
    WPF实现3D翻转的动画效果
  • 原文地址:https://www.cnblogs.com/yydcdut/p/4823133.html
Copyright © 2020-2023  润新知