NotificationManagerCompat 如何获得通知已经被取消或者已经过期了的状态
时间: 2023-08-14 14:03:14 浏览: 30
在Android中,如果通知被取消或者已经过期,系统会发送一个广播给应用。你可以在你的应用中注册这个广播来获得通知的状态。具体来说,你需要在AndroidManifest.xml文件中添加以下代码:
```xml
<receiver android:name=".NotificationDismissedReceiver">
<intent-filter>
<action android:name="android.intent.action.NOTIFICATION_DELETED" />
</intent-filter>
</receiver>
```
然后,你需要创建一个BroadcastReceiver来处理这个广播:
```java
public class NotificationDismissedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.NOTIFICATION_DELETED")) {
int notificationId = intent.getIntExtra("android.intent.extra.NOTIFICATION_ID", -1);
if (notificationId != -1) {
// Notification has been dismissed or expired
}
}
}
}
```
在onReceive()方法中,你可以根据notificationId来判断通知的状态。如果notificationId为-1,表示通知已经过期了;否则,表示通知被取消了。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)