使用NotificationListenerService来监听手机通知,未接电话、未读消息什么的。广告都能监听到
首先写一个类继承NotificationListenerService
这里需要用户手机开启权限
public class NotificationCollectorService extends NotificationListenerService {
//来通知时的调用
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
super.onNotificationPosted(sbn);
Notification notification = sbn.getNotification();
if (notification == null) {
return;
}
Bundle extras = notification.extras;
String content = "";
if (extras != null) {
// 获取通知标题
String title = extras.getString(Notification.EXTRA_TITLE, "");
// 获取通知内容
content = extras.getString(Notification.EXTRA_TEXT, "");
Log.i("包名:",sbn.getPackageName()+"标题:"+title+"内容:"+content);
}
switch (sbn.getPackageName()){
case "com.tencent.mm":
Log.i("微信",content);
break;
case "com.android.mms":
Log.i("短信信",content);
break;
case "com.tencent.mqq":
Log.i("qq",content);
break;
case "com.tencent.tim":
Log.i("tim",content);
break;
case "com.android.incallui":
Log.i("电话",content);
break;
}
}
//删除通知时的调用
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
super.onNotificationRemoved(sbn);
Notification notification = sbn.getNotification();
if (notification == null) {
return;
}
Bundle extras = notification.extras;
String content = "";
if (extras != null) {
// 获取通知标题
String title = extras.getString(Notification.EXTRA_TITLE, "");
// 获取通知内容
content = extras.getString(Notification.EXTRA_TEXT, "");
Log.i("删包名:",sbn.getPackageName()+"标题:"+title+"内容:"+content);
}
switch (sbn.getPackageName()){
case "com.tencent.mm":
Log.i("删微信",content);
break;
case "com.android.mms":
Log.i("删短信",content);
break;
case "com.tencent.mqq":
Log.i("删qq",content);
break;
case "com.tencent.tim":
Log.i("删tim",content);
break;
case "com.android.incallui":
Log.i("删电话",content);
break;
}
}
}
在清单文件中注册*****必须要写的
<service android:name=".NotificationCollectorService"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
在mainActivity中需要加入是否授权的逻辑判断。在这里解决了一个问题,就是在第一次打开应用可以正常监听,但是放用户杀死这个进程之后,就兼听不到了,因为 NotificationListenerService
被杀后再次启动时,并没有去 bindService
,所以导致监听效果无效。。。解决办法可就是把应用的NotificationListenerService实现类disable再enable,即可触发系统rebind操作---详见代码,注释很清晰
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!isNotificationListenerEnabled(this)){
openNotificationListenSettings();
}
toggleNotificationListenerService();
}
//检测通知监听服务是否被授权
public boolean isNotificationListenerEnabled(Context context) {
Set<String> packageNames = NotificationManagerCompat.getEnabledListenerPackages(this);
if (packageNames.contains(context.getPackageName())) {
return true;
}
return false;
}
//打开通知监听设置页面
public void openNotificationListenSettings() {
try {
Intent intent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
} else {
intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
}
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
//把应用的NotificationListenerService实现类disable再enable,即可触发系统rebind操作
private void toggleNotificationListenerService() {
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(
new ComponentName(this, NotificationCollectorService.class),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
pm.setComponentEnabledSetting(
new ComponentName(this, NotificationCollectorService.class),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
}
}
OK。这样就可以监听到手机的通知栏信息了。效果图我就不贴了。大家可以自己实现来看看