NotificationListenerService截取通知

NotificationListenerService截取通知

十二月 14, 2018

此服务可以截取手机app上面的通知悬浮框内容

1:新建一个服务MessageNotificationService实现 NotificationListenerService

public class MessageNotificationService extends NotificationListenerService {
@Override
public void onNotificationPosted(final StatusBarNotification sbn) {
    final Notification mNotification = sbn.getNotification();
    if (mNotification != null) {
      Log.e("返回通知发起的时间=", sbn.getPostTime()+"");
      Log.e("发送通知的包名=",sbn.getPackageName()+"");
      Log.e("通知内容=",mNotification.tickerText+"");

}

@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
    // TODO 自动生成的方法存根
    Log.e("--------------","--------------------------");
}

}

2: AndroidMainifest.xml中配置

<service
     android:name="xx.xx.xx.service.MessageNotificationService"
     android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
     <intent-filter>
         <action android:name="android.service.notification.NotificationListenerService">
         </action>
     </intent-filter>
 </service>

3: 获取手机通知服务,要打开app对应得”通知使用权”
// 跳转到通知使用权
Intent intent = new Intent(“android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS”);
startActivity(intent);


作者:杰骜不驯丶
版权声明:本文为博主原创文章,转载请附上博文链接!