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