Android 监听系统中消息通知事件

2024-02-09 20:58

本文主要是介绍Android 监听系统中消息通知事件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

0. 学习文章

参考了下面Blog 完全没有任何多余的代码

https://blog.csdn.net/wanghang1208/article/details/49905403

原来百度卫士的通知栏收纳是类似这样的原理完成的,很不错.

1.演示结果

数据源头
这里写图片描述

监听到的log

03-24 14:37:04.264 6155-6155/com.lava.noticeobser D/MyNotificationListenService: onNotificationRemoved
03-24 14:39:19.928 6155-6155/com.lava.noticeobser D/MyNotificationListenService: onNotificationPosted
03-24 14:39:19.928 6155-6155/com.lava.noticeobser I/MyNotificationListenService: notify msg: title=北方多地气温创新高,南方新一轮降雨来袭,详情→ ,when=1521873559186 ,contentTitle=升温幅度明显! ,contentText=北方多地气温创新高,南方新一轮降雨来袭,详情→ ,contentSubtext=
03-24 14:39:20.235 6155-6155/com.lava.noticeobser D/MyNotificationListenService: onNotificationPosted
03-24 14:39:20.235 6155-6155/com.lava.noticeobser I/MyNotificationListenService: notify msg: title=这些美食无论如何请在开春吃→ ,when=1521873560030 ,contentTitle=错过等一年! ,contentText=这些美食无论如何请在开春吃→ ,contentSubtext=
03-24 14:39:54.276 6155-6155/com.lava.noticeobser D/MyNotificationListenService: onNotificationPosted
03-24 14:39:54.276 6155-6155/com.lava.noticeobser I/MyNotificationListenService: notify msg: title=正在保存屏幕截图...  ,when=1521873594184 ,contentTitle=正在保存屏幕截图... ,contentText=正在保存屏幕截图。 ,contentSubtext=
03-24 14:39:55.156 6155-6155/com.lava.noticeobser D/MyNotificationListenService: onNotificationPosted
03-24 14:39:55.156 6155-6155/com.lava.noticeobser I/MyNotificationListenService: notify msg: title=正在保存屏幕截图...  ,when=1521873595096 ,contentTitle=已抓取屏幕截图。 ,contentText=点按即可查看您的屏幕截图。 ,contentSubtext=

2.源码

  1. MyNotificationListenService 负责监听消息
package com.lava.noticeobser;import android.app.Notification;
import android.os.Build;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;import java.lang.reflect.Field;public class MyNotificationListenService extends NotificationListenerService {private static final String TAG = MyNotificationListenService.class.getSimpleName();@Overridepublic void onNotificationPosted(StatusBarNotification sbn) {super.onNotificationPosted(sbn);Log.d(TAG, "onNotificationPosted");Notification n = sbn.getNotification();if (n == null) {return;}// 标题和时间String title = "";if (n.tickerText != null) {title = n.tickerText.toString();}long when = n.when;// 其它的信息存在一个bundle中,此bundle在android4.3及之前是私有的,需要通过反射来获取;android4.3之后可以直接获取Bundle bundle = null;if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR2) {// android 4.3try {Field field = Notification.class.getDeclaredField("extras");bundle = (Bundle) field.get(n);} catch (NoSuchFieldException e) {e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR2) {// android 4.3之后bundle = n.extras;}// 内容标题、内容、副内容String contentTitle = bundle.getString(Notification.EXTRA_TITLE);if (contentTitle == null) {contentTitle = "";}String contentText = bundle.getString(Notification.EXTRA_TEXT);if (contentText == null) {contentText = "";}String contentSubtext = bundle.getString(Notification.EXTRA_SUB_TEXT);if (contentSubtext == null) {contentSubtext = "";}Log.i(TAG, "notify msg: title=" + title + " ,when=" + when+ " ,contentTitle=" + contentTitle + " ,contentText="+ contentText + " ,contentSubtext=" + contentSubtext);}@Overridepublic void onNotificationRemoved(StatusBarNotification sbn) {super.onNotificationRemoved(sbn);Log.d(TAG, "onNotificationRemoved");}
}
  1. Mainfest.xml 给 MyNotificationListenService 打辅助
        <service
            android:name=".MyNotificationListenService"android:label="mynotifyservice"android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" ><intent-filter><action android:name="android.service.notification.NotificationListenerService" /></intent-filter></service>
  1. 开启服务
package com.lava.noticeobser;import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);startNotificationListenService();}// 发送通知public void sendNotice(View view) {NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);Notification n;if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {// android 3.0之前n = new Notification(R.mipmap.ic_launcher, "title",System.currentTimeMillis());} else {// android 3.0之后n = new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher).setTicker("title").setContentTitle("content title").setContentText("content text").setSubText("sub text").setWhen(System.currentTimeMillis()).build();}manager.notify(0, n);}// 跳转服务通知的权限界面public void settingsNotice(View view) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");startActivity(intent);} else {Toast.makeText(MainActivity.this, "手机的系统不支持此功能", Toast.LENGTH_SHORT).show();}}// 启动监听消息通知服务private void startNotificationListenService() {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {Intent intent = new Intent(MainActivity.this,MyNotificationListenService.class);startService(intent);} else {Toast.makeText(MainActivity.this, "手机的系统不支持此功能", Toast.LENGTH_SHORT).show();}}
}
  1. 简单粗暴的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.lava.noticeobser.MainActivity"android:orientation="vertical"><Button
        android:onClick="sendNotice"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="发通知" /><Button
        android:onClick="settingsNotice"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="开启监听权限" />
</LinearLayout>

这篇关于Android 监听系统中消息通知事件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/695269

相关文章

Android实现图片浏览功能的示例详解(附带源码)

《Android实现图片浏览功能的示例详解(附带源码)》在许多应用中,都需要展示图片并支持用户进行浏览,本文主要为大家介绍了如何通过Android实现图片浏览功能,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

在Android中使用WebView在线查看PDF文件的方法示例

《在Android中使用WebView在线查看PDF文件的方法示例》在Android应用开发中,有时我们需要在客户端展示PDF文件,以便用户可以阅读或交互,:本文主要介绍在Android中使用We... 目录简介:1. WebView组件介绍2. 在androidManifest.XML中添加Interne

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Go语言并发之通知退出机制的实现

《Go语言并发之通知退出机制的实现》本文主要介绍了Go语言并发之通知退出机制的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1、通知退出机制1.1 进程/main函数退出1.2 通过channel退出1.3 通过cont

RabbitMQ消息总线方式刷新配置服务全过程

《RabbitMQ消息总线方式刷新配置服务全过程》SpringCloudBus通过消息总线与MQ实现微服务配置统一刷新,结合GitWebhooks自动触发更新,避免手动重启,提升效率与可靠性,适用于配... 目录前言介绍环境准备代码示例测试验证总结前言介绍在微服务架构中,为了更方便的向微服务实例广播消息,

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class