NotificationListenerService截获通知

 此服务可以截取手机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);

发布了45 篇原创文章 · 获赞 22 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_24542767/article/details/103505005