【Android开发--新手必看篇】Notification通知栏

    技术2022-07-11  86

    Android笔记

    ​ ——通知栏 【若对该知识点有更多想了解的,欢迎私信博主~~】

    通知栏:Notification

    制作:
    //发送通知 private void startNotification(String word,String word2) { //创建通知栏管理工具,调用getSystemService()方法获取系统的NotificationManager服务 notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); //实例化通知栏构造器 NotificationCompat.Builder builder=new NotificationCompat.Builder(this); //为builder设置各种属性 builder.setContentTitle(word) .setContentText(word2) .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)) .setSmallIcon(R.mipmap.ic_launcher) .setTicker("这是提示文本") .setDefaults(Notification.DEFAULT_ALL); //发送通知(id:通知序号,id若唯一时则永远保持第一个) notificationManager.notify(1,builder.build()); } //取消通知 private void endNotification(){ //取消通知 notificationManager.cancel(1); }
    使用:
    NotificationManager notificationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main10); startNotification("这是标题","这是内容"); }
    builder的方法:
    方法说明setDefaults()设置通知LED灯、音乐、震动等setAutoCancel()设置点击通知后,状态栏自动删除通知setContentTitle()设置通知标题setContentText()设置通知内容setSmallIcon()为通知设置图标setLargeIcon()为通知设置大图标setTicker()设置通知在状态栏的提示文本setContentIntent()设置点击通知后将要启动的程序组件对应的PendingIntentsetSound()设置通知的自定义声音setVibrate()设置通知的自定义震动
    通知方式:
    属性说明Notification.DEFAULT_SOUND设置使用默认声音Notification.DEFAULT_LIGHTS设置使用默认闪光灯Notification.DEFAULT_VIBRATE设置使用默认震动Notification.DEFAULT_ALL设置使用默认声音、震动、闪光灯
    注:
    1.在使用闪光灯和振动器时需要添加权限
    <!--添加操作闪光灯的权限--> <uses-permission android:name="android.permission.FLASHLIGHT"/> <!--添加操作振动器的权限--> <uses-permission android:name="android.permission.VIBRATE"/>
    2.高版本API可能收不到通知
    Processed: 0.012, SQL: 9