创建和使用通知的相关Android类及其方法的作用
时间: 2023-06-17 07:04:38 浏览: 45
在Android应用开发中,通知(Notification)是一种非常重要的组件,它能够在应用程序外部向用户发送消息。通知可以显示在状态栏、锁屏界面或者弹出一个悬浮窗口等等。通知可以用于提醒用户新消息、任务完成、更新等等。创建和使用通知的相关Android类及其方法如下:
1. NotificationManager类:负责管理通知的类,可以通过该类发送、更新、取消通知。
2. NotificationCompat类:提供了兼容所有Android版本的通知构建方法和样式。
3. Notification.Builder类:用于构建通知的类,可以设置通知的图标、标题、内容、声音、震动、LED灯等属性。
4. PendingIntent类:用于在通知被点击时启动一个Activity或者Service等。
5. NotificationChannel类:用于设置通知渠道,可以设置通知优先级、声音、震动、LED灯等属性。
常用方法:
1. NotificationManager.notify(int id, Notification notification):发送通知。
2. NotificationManager.cancel(int id):取消指定id的通知。
3. NotificationCompat.Builder.setSmallIcon(int icon):设置通知的小图标。
4. NotificationCompat.Builder.setContentTitle(CharSequence title):设置通知的标题。
5. NotificationCompat.Builder.setContentText(CharSequence text):设置通知的内容。
6. NotificationCompat.Builder.setPriority(int priority):设置通知的优先级。
7. NotificationCompat.Builder.setSound(Uri sound):设置通知的声音。
8. NotificationCompat.Builder.setVibrate(long[] pattern):设置通知的震动。
9. NotificationCompat.Builder.setLights(int argb, int onMs, int offMs):设置通知的LED灯。
10. NotificationCompat.Builder.setContentIntent(PendingIntent intent):设置通知被点击时的意图。
11. NotificationChannel.enableVibration(boolean vibration):设置通知渠道是否震动。
12. NotificationChannel.enableLights(boolean lights):设置通知渠道是否LED灯。
13. NotificationChannel.setSound(Uri sound, AudioAttributes attributes):设置通知渠道的声音。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)