Notification 学习

第一行代码中的代码是基于android 7.0进行编写,API的调用已经落后较多

Notification 常用写法:

    private void showNotification() {
        Intent intent = FragmentTestActivity.formatIntent(this);
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
        NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new Notification.Builder(this)
                .setContentTitle("title")
                .setContentText("test")
                .setWhen(System.currentTimeMillis())        //show time on notification
                .setSmallIcon(R.mipmap.ic_launcher_round)   //icon in the status bar
                .setAutoCancel(true)                        //cancel self when click notification
                .setContentIntent(pi)                       //set the intent handled when click notification
                .build();

        if(notiManager != null){
            notiManager.notify(1, notification);
        }
    }

手动关闭notification:

        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if(nm != null){
            //1 is the id, which should be the same int value when `notify(int id, Notification notification) called` 
            nm.cancel(1);
        }

NotificationChannel: 8.0 新增特性

下午学习

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,467评论 2 59
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,216评论 25 709
  • 杏花朵朵芯中红, 枝上翘首羞争春, 本是孤挑素凉意, 谁却误入杏花村。
    飘飘摇阅读 1,735评论 0 1
  • 母亲再关键,也取代不了父亲 孩子成长路,父爱不可缺 每个孩子自面对世界开始,他的成长规律是需要的母性之爱日渐减少,...
    十里不如你阅读 5,489评论 0 1