zoukankan      html  css  js  c++  java
  • 通知

    通知(Notification)

    1、通知的基本用法

       //创建 NotificationManager 实例
       NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
       Notification notification = new NotificationCompat.Builder(this)
               .setContentTitle("This is title")  //标题
               .setContentText("This is content")  //正文内容
               .setWhen(System.currentTimeMillis()) //通知被创建的时间
               .setSmallIcon(R.mipmap.ic_launcher)  //状态栏通知图标
               //通知界面的图标
               .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
               .build();  
              //发送通知
               manager.notify(1, notification);
    

    1.jpg

    Android 6.0:6.0.png 7.0:7.0.png

    注意:也可以直接创建 NotificationCompat.Builder 实例来一个个的设置方法,

    最后修改下 manager.notify(1,builder.builder()) 就Ok。

    2、给通知添加点击事件

    <1> 创建 PendingIntent 实例

       Intent intent =new Intent(this,NotificationActivity.class);
                                                       //指定意图
       PendingIntent pi=PendingIntent.getActivity(this,0,intent,0);
    

    <2> 调用 NotificationCompat.Builder 的 setContentIntent() 方法 ,传入 pi 就行

     .setContentIntent(pi)
    

    <3> 点击通知后取消通知栏显示

    type1: NotificationCompat.Builder
      .setAutoCancel(true)
    

    type2: 创建通知管理器,取消指定通知

       NotificationManager manger=(NotificationManager)getSystemService(
               NOTIFICATION_SERVICE);
       manger.cancel(1);
    

    cancel方法的参数1就是发送通知时传入的 Id

    3、通知栏的进阶技巧

    <1>设置提示声音

       //设置声音                          路径:是系统的内置铃声目录
       .setSound( Uri.fromFile(new File("/system/media/audio/ringtones/ANDROMEDA.ogg")))
    

    <2>设置震动

    先注册权限
    <uses-permission android:name="android.permission.VIBRATE"/>
    

    再,

      //设置震动             静止,震动,静止, 震动。。。。。
      .setVibrate(new long[]{0,   1000, 1000, 1000})
    

    <3>设置灯光

        //设置灯光   颜色    亮的时间  暗的时间
        .setLights(Color.RED,300,     300)
    

    还可以直接使用通知的默认效果,它会根据当前的手机环境来决定进行怎样的效果。

    builder.setDefaults(NotificationCompat.DEFAULT_ALL);
    

    4、通知的高级功能

    <1> 长文本

        //  长文字显示
        builder.setStyle(new  NotificationCompat.BigTextStyle().bigText("" +
                "111111111111111111111222222222222222222222223333333333333333333" +
                "4444444444444444444444444444445555555555555555555555555555555" +
                "666666666666666666666666666666"));
    

    <2>图片显示

      // 图片上显示 ,注意图片不能太大
       builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture
               (BitmapFactory.decodeResource(getResources(),R.drawable.google)));
    

    2.png 3.jpg

    <3> setPriority() 方法

    它接收一个整型参数用于设置这条通知重要程度,一共有5个可选值

    PRIORITY_DEFAULT,PRIORITY_MIN,PRIORITY_LOW,PRIORITY_HIGH,PRIORITY_MAX

    当设置为 PRIORITY_MAX 后的显示效果 。 4.png

    文章部分内容摘自网络和图书,如有侵权,请联系我。
  • 相关阅读:
    解决流氓软件的工具,做个记录
    目前使用较好的网盘搜索引擎
    网页截图分段保存
    国外统计学课程主页Statistical Books, Manuals and Journals
    notepad++ TextFX替代
    只用 4 个月打造机器学习必备技能,这位工程师成功翻转职涯人生
    时序差分学习
    来自NVIDIA开源的pix2pixHD,将Image-to-Image Translation带到了另一个境界
    关于HTML5,最牛逼的10本书!
    IDEA配置和插件
  • 原文地址:https://www.cnblogs.com/-Tiger/p/7301268.html
Copyright © 2011-2022 走看看