• Notification状态栏显示信息


    Notification即通知,用于在通知栏显示提示信息。

    在API Level > 11,Notification类中的一些方法被Android声明deprecated(弃用),而在API Level > 16, Notification类又有新的方法实现。

    创建Notification通知:

    1、获取Notification管理器

    1 NotificationManager notiMgr= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

    2、新建一个Notification,设置状态栏显示样式

    1 private Notification note;
    2 //API 11以下
    3 note = new Notification(R.drawable.ico_launcher ,"显示于屏幕顶端状态栏的文本", System.currentTimeMillis());
    4 //API 11及以上
    5 Notification.Builder builder = new Notification.Builder(nowContext).setTicker("显示于屏幕顶端状态栏的文本")
    6                                                .setSmallIcon(R.drawable.ic_laucher);

    API 11以上版本中,状态栏显示的样式跟下拉通知栏中显示的样式,可以一起设置,就是通过Notification.Builder类来实现,这里的Builder只调用了两个方法来设置状态栏显示样式。

    3、设置Notification标志位(非必要步骤)

    //FLAG_ONGOING_EVENT表明有程序在运行,该Notification不可由用户清除
    note.flags = Notification.FLAG_ONGOING_EVENT;

    4、设置点击Notification后的触发事件

    //通过Intent,使得点击Notification之后会启动新的Activity
    Intent i = new Intent(nowContext, AnotherActivity.class);
    //该标志位表示如果Intent要启动的Activity在栈顶,则无须创建新的实例
    i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); //i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    pendingIntent = PendingIntent.getActivity(nowContext, 100, i, PendingIntent.FLAG_UPDATE_CURRENT);

    5、设置Notification在通知栏里的样式
    (1)系统默认样式

    //API 11以下:
    note.setLatestEventInfo(nowContext, "take me to your heart", "Micheal learn to rock", pendingIntent);
    
    //API 11~15:
    note = builder.setContentIntent(pendingIntent).setContentTitle("title").setContentText("text").getNotification();

    //API 16及以上,build()方法要求API 16及以上 note = builder.setContentIntent(pendingIntent).setContentTitle("title").setContentText("text").build();
    if (Build.VERSION.SDK_INT < 16) {
        nm.notify(MY_NOTIFICATION_ID, notificationBuilder.getNotification());
    } else {
        nm.notify(MY_NOTIFICATION_ID, notificationBuilder.build());
    }


    (2)自定义样式:

    自定义样式,就是让Notification在通知栏显示成自定义的xml布局
    应当注意的是,Notification的自定义样式,只支持以下可视组件:
    FrameLayout, LinearLayout, RelativeLayout
    TextView, Button, AnalogClock, ImageView, ImageButton, Chronometer, ProgressBar

    RemoteView view = new RemoteView(nowActivity.getPackageName(), R.layout.note_layout);
    //API 11以下
    note.contentView = view;
    note.contentIntent = pendingIntent;
    //API 16及以上,又是build()方法导致的,汗。。
    note = builder.setContent(view).setContentIntent(pendingIntent).build();

    这个步骤里有一个很值得注意的地方:pendingIntent被设置为note的contentIntent的值,就意味着点击了这个通知才会触发该Intent。

    那么如果只是想让自定义布局里的某个按钮触发呢?比如说,弄了一个音乐播放器,Service负责播放音乐,Notification显示当前播放进度和一些简单的暂停按钮、上一首、下一首按钮,让用户不用再打开界面就可以通过Notification上的按钮操纵音乐播放。

    假设说想让自定义布局里的一个id为R.id.button1的按钮来触发这个Intent,可以如下操作:

    view.setOnClickPendingIntent(R.id.button1, pendingIntent);//在上面创建RemoteView实例后加上这句

    然后注意,pendingIntent已经绑定到按钮上了,上面Notificatiion实例中,设置contentIntent的语句要去掉。

    6、发布该通知,第一个参数为该notification的ID

    noteMng.notify(10, note);

     7、取消状态栏上显示的消息(例如当程序结束时),第一个参数为该notification的ID

    noteMng.cancel(10);
  • 相关阅读:
    两数之和
    IntelliJ IDEA为类和方法自动添加注释
    IDEA main 函数的快捷键
    Mac终端开启代理
    Pycharm节能模式
    使用正则表达式替换构造字典
    使用代理爬取微信文章
    利用 Scrapy 爬取知乎用户信息
    Scrapy选择器的用法
    Scrapy命令行基本用法
  • 原文地址:https://www.cnblogs.com/ghimtim/p/4764383.html
Copyright © 2020-2023  润新知