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中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

C#如何动态创建Label,及动态label事件

《C#如何动态创建Label,及动态label事件》:本文主要介绍C#如何动态创建Label,及动态label事件,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#如何动态创建Label,及动态label事件第一点:switch中的生成我们的label事件接着,

SpringKafka消息发布之KafkaTemplate与事务支持功能

《SpringKafka消息发布之KafkaTemplate与事务支持功能》通过本文介绍的基本用法、序列化选项、事务支持、错误处理和性能优化技术,开发者可以构建高效可靠的Kafka消息发布系统,事务支... 目录引言一、KafkaTemplate基础二、消息序列化三、事务支持机制四、错误处理与重试五、性能优

SpringIntegration消息路由之Router的条件路由与过滤功能

《SpringIntegration消息路由之Router的条件路由与过滤功能》本文详细介绍了Router的基础概念、条件路由实现、基于消息头的路由、动态路由与路由表、消息过滤与选择性路由以及错误处理... 目录引言一、Router基础概念二、条件路由实现三、基于消息头的路由四、动态路由与路由表五、消息过滤

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

Android WebView无法加载H5页面的常见问题和解决方法

《AndroidWebView无法加载H5页面的常见问题和解决方法》AndroidWebView是一种视图组件,使得Android应用能够显示网页内容,它基于Chromium,具备现代浏览器的许多功... 目录1. WebView 简介2. 常见问题3. 网络权限设置4. 启用 JavaScript5. D

Android如何获取当前CPU频率和占用率

《Android如何获取当前CPU频率和占用率》最近在优化App的性能,需要获取当前CPU视频频率和占用率,所以本文小编就来和大家总结一下如何在Android中获取当前CPU频率和占用率吧... 最近在优化 App 的性能,需要获取当前 CPU视频频率和占用率,通过查询资料,大致思路如下:目前没有标准的

spring @EventListener 事件与监听的示例详解

《spring@EventListener事件与监听的示例详解》本文介绍了自定义Spring事件和监听器的方法,包括如何发布事件、监听事件以及如何处理异步事件,通过示例代码和日志,展示了事件的顺序... 目录1、自定义Application Event2、自定义监听3、测试4、源代码5、其他5.1 顺序执行