Android学习笔记
Android 开发者基础知识 (Java) —— Google Developers 培训团队
文章目录
第3单元 在后台运行
第8课 闹钟和调度程序
89. 通知
你会做什么
- 创建一个应用程序,当用户点击应用程序中的按钮时发送通知。
- 从应用中的按钮和通知内的操作按钮更新通知。
这里也贴一篇笔者关于通知Notification的文章
https://blog.csdn.net/weixin_44226181/article/details/126206172
89.1 创建基本通知
-
创建项目
布局
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/notify" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Notify Me!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.dingjiaxiong.notifyme; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Button button_notify; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button_notify = findViewById(R.id.notify); button_notify.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendNotification(); } }); } public void sendNotification() { } }
-
创建通知通道
在 Android 设备上的“设置”应用中,用户可以调整他们收到的通知。从 Android 8.0(API 级别 26)开始,您的代码可以将应用的每个通知分配给用户可自定义的 通知通道:
- 每个通知通道代表一种通知类型。
- 在您的代码中,您可以在每个通知通道中对多个通知进行分组。
- 对于每个通知通道,您的应用程序都会为通道设置行为,并且该行为将应用于通道中的所有通知。例如,您的应用可能会将频道中的通知设置为播放声音、闪烁灯光或振动。
- 无论您的应用程序为通知通道设置什么行为,用户都可以更改该行为,并且用户可以完全关闭您的应用程序的通知。
在运行 Android 8.0(API 级别 26)或更高版本的 Android 设备上,您在应用程序中创建的通知渠道会在设备设置应用程序的应用程序通知下显示为类别。
package com.dingjiaxiong.notifyme; import androidx.appcompat.app.AppCompatActivity; import android.app.NotificationChannel; import android.app.NotificationManager; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private static final String PRIMARY_CHANNEL_ID = "primary_notification_channel"; private NotificationManager mNotifyManager; private Button button_notify; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button_notify = findViewById(R.id.notify); button_notify.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { sendNotification(); } }); } public void sendNotification() { } public void createNotificationChannel() { mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { // Create a NotificationChannel NotificationChannel notificationChannel = new NotificationChannel(PRIMARY_CHANNEL_ID, "Mascot Notification", NotificationManager .IMPORTANCE_HIGH); notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.RED); notificationChannel.enableVibration(true); notificationChannel.setDescription("Notification from Mascot"); mNotifyManager.createNotificationChannel(notificationChannel); } } }
-
构建第一个通知
-
添加内容intent并关闭通知
-
为通知添加优先级和默认值以实现向后兼容性
89.2 更新或取消通知
- 添加更新按钮和取消按钮
- 实现取消和更新通知方法
- 切换按钮状态
89.3 添加通知动作按钮
- 实现一个调用updateNotification( )的广播接收器
- 为更新操作创建一个图标
- 将更新操作添加到通知中
项目地址:https://github.com/google-developer-training/android-fundamentals-apps-v2/tree/master/NotifyMe
89.4 小结
通知是您可以在应用的正常 UI 之外向用户显示的消息:
- 通知为您的应用程序提供了一种与用户交互的方式,即使在应用程序未运行时也是如此。
- 当 Android 发出通知时,通知首先以图标的形式出现在设备的通知区域中。
- 要指定通知的 UI 和操作,请使用
NotificationCompat.Builder
. - 要创建通知,请使用
NotificationCompat.Builder.build()
。 - 要发出通知,请使用
NotificationManager.notify()
将通知对象传递给 Android 运行时系统。 - 要使更新或取消通知成为可能,请将通知 ID 与通知相关联。
- 通知可以有几个组件,包括一个小图标(
setSmallIcon()
, 必需的);标题(setContentTitle()
);和详细的文字(setContentText()
)。 - 通知还可以包括待处理的意图、扩展样式、优先级等。