由于自动化测试需要,我们希望能够监听Android手机中的推送消息来实现发送推送后的客户端自动检查。下面就来看看我们是如何实现客户端统计消息接收的。
大致思路是在Android手机上安装一个程序,这个程序可以监听手机接收到的消息,然后打印出来,比如打印到logcat。经查,如果要实现这个功能,就要用到NotificationListenerService。
先看主程序:
package com.xingshulin.xsltestcontroller;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 通知栏监控器开关
Button notificationMonitorOnBtn = (Button)findViewById(R.id.notification_monitor_on_btn);
notificationMonitorOnBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//
if (!isEnabled()) {
startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
} else {
Toast toast = Toast.makeText(getApplicationContext(), "监控器开关已打开", Toast.LENGTH_SHORT);
toast.show();
}
}
});
Button notificationMonitorOffBtn = (Button)findViewById(R.id.notification_monitor_off_btn);
notificationMonitorOffBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//
if (isEnabled()) {
startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
} else {
Toast toast = Toast.makeText(getApplicationContext(), "监控器开关已关闭", Toast.LENGTH_SHORT);
toast.show();
}
}
});
}
// 判断是否打开了通知监听权限
private boolean isEnabled() {
String pkgName = getPackageName();
final String flat = Settings.Secure.getString(getContentResolver(), "enabled_notification_listeners");
if (!TextUtils.isEmpty(flat)) {
final String[] names = flat.split(":");
for (int i = 0; i < names.length; i++) {
final ComponentName cn = ComponentName.unflattenFromString(names[i]);
if (cn != null) {
if (TextUtils.equals(pkgName, cn.getPackageName())) {
return true;
}
}
}
}
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
主程序很简单,就是安排两个按键,一个用来打开通知消息监听,一个用来关闭。点击打开监听按键后,如果之前并没有为APP打开过通知消息监听的权限,会跳转到通知消息监听权限界面,有个按钮会让你能打开权限。

实现就是程序中的:
if (!isEnabled()) {
startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
}
isEnabled()方法是用来判断通知消息监听权限是否已经打开了。方法中的"enabled_notification_listeners"为数据库字段,我们可以通过它的值来判断我们的APP是否有通知使用权。
下面是关键的NotificationListenerService 的实现:
package com.xingshulin.xsltestcontroller;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.os.Bundle;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.util.Log;
@SuppressLint("NewApi")
public class NotificationMonitorService extends NotificationListenerService {
// 在收到消息时触发
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
// TODO Auto-generated method stub
Bundle extras = sbn.getNotification().extras;
// 获取接收消息APP的包名
String notificationPkg = sbn.getPackageName();
// 获取接收消息的抬头
String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
// 获取接收消息的内容
String notificationText = extras.getString(Notification.EXTRA_TEXT);
Log.i("XSL_Test", "Notification posted " + notificationTitle + " & " + notificationText);
}
// 在删除消息时触发
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
// TODO Auto-generated method stub
Bundle extras = sbn.getNotification().extras;
// 获取接收消息APP的包名
String notificationPkg = sbn.getPackageName();
// 获取接收消息的抬头
String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
// 获取接收消息的内容
String notificationText = extras.getString(Notification.EXTRA_TEXT);
Log.i("XSL_Test", "Notification removed " + notificationTitle + " & " + notificationText);
}
}
有两个需要Override的方法,onNotificationPosted和onNotificationRemoved。
onNotificationPosted是在接收到通知消息时触发
onNotificationRemoved是在通知消息被删除时触发,比如通知栏中左划或右划消息。
在方法中我们可以获取几乎所有消息的信息,如APP的包名,抬头和消息内容等。这些信息恰恰是我们做测试时需要判断的。
最后是AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xingshulin.xsltestcontroller"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/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=".NotificationMonitorService"
android:label="NotificationMonitor"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
</application>
</manifest>
这样,我们就可以通过这个APP获取通知消息的动作了,当然,我们同样也就可以只获得我们需要APP的消息了(通过在NotificationMonitorService 中过滤接收消息APP的包名):

网友评论