NotificationListenerService 监听应用程序消息
来源:互联网 发布:数据库表重命名as语句 编辑:程序博客网 时间:2024/06/13 08:54
最近在写一个手机弹幕的功能,主要实现监听手机应用接受到的消息,并以弹幕的形式展示给用户,这就涉及到了NotificationListenerService的应用,刚开始一点也不懂,上网各种查资料,功夫不负有心人,折腾了半天加一个晚上,终于实现了监听功能,下面我就给大家分享一下我的学习经验,希望大家不要像我一样走弯路。
1、继承NotificationListenerService 实现里面的方法:
@Override public void onNotificationPosted(StatusBarNotification sbn) {} @Override public void onNotificationRemoved(StatusBarNotification sbn) {}
这两个方法 通过对象 sbn 获取消息内容。
2、在AndroidManifest.xml中注册Service并声明相关权限:
<service android:name=".MyNotificationService" android:label="HaHa" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"><intent-filter> <action android:name="android.service.notification.NotificationListenerService" /></intent-filter></service>
这个很重要,权限
android:permission=”android.permission.BIND_NOTIFICATION_LISTENER_SERVICE”必须加,否则服务启动了,但是还是接受不了消息。
3、打开监听引用消息Notification access,在里面把你写的服务名字打钩,这样才算完成,如果你在手机上找不到这个设置,可以自己手动添加:
Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);startActivity(intent);
好了,就这三个步骤,下面我给大家附上主要代码:
1、MyNotificationService类,这里在编译器上面可能有点坑,反正我是遇到了,就是你可能代码内容完全正确,但是编译器就是不打印接收到的消息日志,我在这个问题上纠结了很久,结果重新创建一个类,把代码复制过去就实现了,太无语。
package com.tielizi.mynotification;import android.app.Service;import android.content.Intent;import android.os.Handler;import android.os.IBinder;import android.os.Message;import android.service.notification.NotificationListenerService;import android.service.notification.StatusBarNotification;import android.util.Log;import android.widget.Toast;public class MyNotificationService extends NotificationListenerService { private MyHandler handler = new MyHandler(); @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("px", "Srevice is open"+"-----"); return super.onStartCommand(intent, flags, startId); } @Override public void onNotificationPosted(StatusBarNotification sbn) { Log.i("px", "Get Message"+"-----"+sbn.getNotification().tickerText.toString()); Message message = handler.obtainMessage(); message.what = 1; handler.sendMessage(message); //不能再这里更新UI,会报错,你可以试试,在子线程中不能更新UI // Toast.makeText(MyNotificationService.this,"怎一个曹字了得!",Toast.LENGTH_SHORT).show(); } class MyHandler extends Handler { @Override public void handleMessage(Message msg) { switch (msg.what){ case 1 : Toast.makeText(MyNotificationService.this,"怎一个曹字了得,终于实现了!",Toast.LENGTH_SHORT).show(); } } }}
2、MainActivity:
package com.tielizi.mynotification;import android.app.Activity;import android.content.Intent;import android.provider.Settings;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;public class MainActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); startService(new Intent(MainActivity.this, MyNotificationService.class));//启动服务 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {//打开监听引用消息Notification access Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS); startActivity(intent); } }); }}
3、权限配置
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tielizi.mynotification" > <uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".MyNotificationService" android:label="HaHa" android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> <intent-filter> <action android:name="android.service.notification.NotificationListenerService" /> </intent-filter> </service> </application></manifest>
好了,就这些了。
源码下载
1 0
- NotificationListenerService 监听应用程序消息
- 使用NotificationListenerService监听通知栏消息
- 【笔记】Android NotificationListenerService监听短信、来电、微信、QQ等通知消息
- NotificationListenerService
- Android通知栏监听之NotificationListenerService
- 关于NotificationListenerService监听时有失败的处理
- Android 通过系统使用NotificationListenerService 监听各种Notification的使用方法
- Android 通过系统使用NotificationListenerService 监听各种Notification的使用方法
- Android 通过系统使用NotificationListenerService 监听各种Notification的使用方法
- android NotificationListenerService监听通知栏(qq 微信 短信)
- Android输入子系统之应用程序注册消息监听过程分析
- 监听SMS消息(转)
- 监听SMS消息
- 消息监听类
- soap消息监听
- 网络消息监听处理
- xmpp 消息监听
- NotificationListenerService使用小结
- jquery datatable 参数
- JVM内存结构解析(一)
- 如何简单地在浏览器中使用阿里云的文件上传功能?
- Android 权限配置
- OpenGl Mip贴图
- NotificationListenerService 监听应用程序消息
- Serializable 作用
- FPGA编程的四种思想
- 【J2SE视频】-JDK安装
- android 一个apk调用另一个apk
- 欢迎使用CSDN-markdown编辑器
- spring实现AOP的两种方式以及实现动态代理方式
- 第13章 PE文件格式 13.1~13.2.2
- 各种音视频编解码学习详解 h264 ,mpeg4 ,aac 等所有音视频格式