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