1.在Manifest中加入以下内容
<service
android:name="com.example.watch.NotificationCollectorService"
android:label="通知栏监听"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" >
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
其中com.example.watch.NotificationCollectorService为自定义的服务名
2.继承NotificationListenerService
public class NotificationCollectorService extends NotificationListenerService {
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
Log.i("123", "open" + "-----" + sbn.toString());
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i("123", "shut" + "-----" + sbn.toString());
}
}
3.让用户在手机中勾选我们的APP(首次使用要求用户勾选,否则service无效)
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
startActivity(intent);