Notification基本操作
Notification的基本操作主要有创建、更新、取消三种。一个Notification的必要属性有三项,如果不设置就会在运行时抛出异常:
- 小图标,通过setSmallIcon()方法设置
- 标题,通过setContentTitle()方法设置
- 内容,通过setContentText()方法设置
除了这三个之外,还应该给Notification设置一个action,这样能直接跳转到其他app的某个activity、启动service或者发送broadcast,可以和用户进行交互。
创建Notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- 获取NotificationManager实例
- 实例化Notification.Builder并且设置属性
- 发送通知
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 添加通知
Notification.Builder notification1 = new Notification.Builder(MainActivity.this);
// 设置通知图标
notification1.setSmallIcon(R.drawable.advise2);
// 设置通知标题
notification1.setContentTitle("通知");
// 设置通知内容
notification1.setContentTitle("查看详细内容");
// 查看详细内容自动关闭
notification1.setAutoCancel(true);
Intent intent = new Intent(MainActivity.this, com.example.notification.ContentActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
notification1.setContentIntent(pendingIntent);
// 发送通知
manager.notify(NOTIFYID_1, notification1.build());
取消notification
// notificationManager.cancel(NOTIFYID_1);
// // 清除ID号为常量NOTIFYID_1的通知
// 清除全部通知
notificationManager.cancelAll();
除了上面这一种还有另外的四种取消通知的方法:
- 点击通知栏的清除按钮,会清除所有可以清除的通知
- 设置了setAutoCancel() 或 FLAG_AUTO_CANCEL 的通知,点击该通知时会清除它
- 通过 NotificationManager 调用 cancel(int id) 方法清除指定 ID 的通知
- 通过 NotificationManager 调用 cancel(String tag, int id) 方法清除指定 TAG 和 ID 的通知
以下是部分代码
main.xml
这里创建两个button用于显示和清除通知
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示通知" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除通知" />
content.xml
用于显示通知
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content"
android:textAppearance="?android:attr/textAppearanceMedium" />
核心代码
package com.example.notification;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
final int NOTIFYID_1 = 124; // 通知的ID
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取通知管理器,用于发送通知
final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 获取显示通知的按钮
Button button1 = findViewById(R.id.button1);
// 为显示通知的按钮添加点击事件监听器
button1.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View v) {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 添加通知
Notification.Builder notification1 = new Notification.Builder(MainActivity.this);
// 设置通知图标
notification1.setSmallIcon(R.drawable.advise2);
// 设置通知标题
notification1.setContentTitle("通知");
// 设置通知内容
notification1.setContentTitle("查看详细内容");
// 查看详细内容自动关闭
notification1.setAutoCancel(true);
Intent intent = new Intent(MainActivity.this, com.example.notification.ContentActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
notification1.setContentIntent(pendingIntent);
// 发送通知
manager.notify(NOTIFYID_1, notification1.build());
}
});
// 获取删除通知按钮
Button button2 = findViewById(R.id.button2);
// 为删除通知按钮添加点击事件监听器
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// notificationManager.cancel(NOTIFYID_1);
// // 清除ID号为常量NOTIFYID_1的通知
// 清除全部通知
notificationManager.cancelAll();
}
});
}
}
以上只是简单的实现了任务栏的通知。
最终效果