Android 监听Notification 被清除实例代码

简介: Android 监听Notification 被清除实例代码
public class NotifiDoWhatReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent == null || context == null) {
            return;
        }
        String action = intent.getAction();
        if(action.equals(NOTIFICATION_DELETED_ACTION)){
            int type = intent.getIntExtra(PUSH_TYPE,-1);
            int notiid  = intent.getIntExtra(PUSH_ID,-1);
            String url = intent.getStringExtra(Constant.DATA);
            if(type==0){
                //apk下载
                ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(notiid);
                if(downCalls.containsKey(url)){
                    downCalls.remove(url);
                }
            }
        }
    }
 
}
 public void initNotification() {
        builder = new NotificationCompat.Builder(mContext)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText("0%")
                .setContentTitle("初心客厅")
                .setProgress(100, 0, false).setDeleteIntent(PendingIntent.getBroadcast(this, NOTIFICATION_ID_APK,
     new Intent(NOTIFICATION_DELETED_ACTION).putExtra(PUSH_ID, NOTIFICATION_ID_APK).putExtra(PUSH_TYPE, 0).putExtra(Constant.DATA, url), 0));
        notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID_APK, builder.build());
    }

参考资料


private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
 
 @Override
 public void onReceive(Context context, Intent intent) {
  if (intent == null || context == null) {
   return;
  }
 
  mNotificationManager.cancel(NOTIFICATION_ID_LIVE);
 
  String type = intent.getStringExtra(PUSH_TYPE);
  if (PUSH_TYPE_LINK.equals(type)) {
   //mNumLinkes = 0;
  } else if (PUSH_TYPE_LIVE.equals(type)) {
   //mNumLives = 0;
  }
  //这里可以重新计数
 }
};
 
 
private void sendLiveNotification() {
 Intent intent = new Intent(NOTIFICATION_CLICK_ACTION);
 
 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
 
 String title = "Push测试";
 mBuilder.setContentTitle(title);
 mBuilder.setTicker(title);
 mBuilder.setContentText("https://233.tv/over140");
 mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
 mBuilder.setSmallIcon(R.drawable.ic_action_cast);
 mBuilder.setDefaults(Notification.DEFAULT_ALL);
 mBuilder.setWhen(System.currentTimeMillis());
 mBuilder.setContentIntent(PendingIntent.getBroadcast(this, NOTIFICATION_ID_LIVE, intent, 0));
 mBuilder.setDeleteIntent(PendingIntent.getBroadcast(this, NOTIFICATION_ID_LIVE, new Intent(NOTIFICATION_DELETED_ACTION).putExtra(PUSH_TYPE, PUSH_TYPE_LIVE), 0));
 
 mNotificationManager.notify(NOTIFICATION_ID_LIVE, mBuilder.build());
}

代码说明


 1、最重要的是setDeleteIntent,这个在API Level 11(Android 3.0) 新增的


 2、注意不要设置setAutoCancel为true,否则监听器接收不到


 3、这里统一了点击通知和消除通知的操作


 4、注意广播在推送前要注册好

实际使用中发现还是有一点问题,比如Service被Kill掉了,通知栏点击就会没有反应了,这块还需要再考虑一下,比如把Broadcast在AndroidMainfest中监听。


以上就是对Android Notification 事件的资料整理,希望能帮助Android开发的朋友。


相关文章
|
11天前
|
Android开发
技术经验分享:Android前后台切换的监听
技术经验分享:Android前后台切换的监听
11 2
|
13天前
|
安全 Java Android开发
使用Unidbg进行安卓逆向实例讲解
使用Unidbg进行安卓逆向实例讲解
16 2
|
21天前
|
JavaScript 前端开发 Android开发
kotlin安卓在Jetpack Compose 框架下使用webview , 网页中的JavaScript代码如何与native交互
在Jetpack Compose中使用Kotlin创建Webview组件,设置JavaScript交互:`@Composable`函数`ComposableWebView`加载网页并启用JavaScript。通过`addJavascriptInterface`添加`WebAppInterface`类,允许JavaScript调用Android方法如播放音频。当页面加载完成时,执行`onWebViewReady`回调。
|
5天前
|
Web App开发 JavaScript 前端开发
Android端使用WebView注入一段js代码实现js调用android
Android端使用WebView注入一段js代码实现js调用android
15 0
|
1月前
|
XML Android开发 数据格式
37. 【Android教程】基于监听的事件处理机制
37. 【Android教程】基于监听的事件处理机制
25 2
|
1月前
|
API Android开发
31. 【Android教程】状态栏通知:Notification
31. 【Android教程】状态栏通知:Notification
15 1
|
2月前
|
移动开发 监控 Android开发
构建高效Android应用:从内存优化到电池寿命代码之美:从功能实现到艺术创作
【5月更文挑战第28天】 在移动开发领域,特别是针对Android系统,性能优化始终是关键议题之一。本文深入探讨了如何通过细致的内存管理和电池使用策略,提升Android应用的运行效率和用户体验。文章不仅涵盖了现代Android设备上常见的内存泄漏问题,还提出了有效的解决方案,包括代码级优化和使用工具进行诊断。同时,文中也详细阐述了如何通过减少不必要的后台服务、合理管理设备唤醒锁以及优化网络调用等手段延长应用的电池续航时间。这些方法和技术旨在帮助开发者构建更加健壮、高效的Android应用程序。
|
16天前
|
存储 算法 Java
Android 进阶——代码插桩必知必会&ASM7字节码操作
Android 进阶——代码插桩必知必会&ASM7字节码操作
39 0
|
16天前
|
开发工具 Android开发
Android 代码自定义drawble文件实现View圆角背景
Android 代码自定义drawble文件实现View圆角背景
19 0
|
16天前
|
XML Java Android开发
Android RecyclerView用代码动态设置item的selector
Android RecyclerView用代码动态设置item的selector
17 0