推送拦截
其他
2018-12-21 20:03:15
阅读次数: 0
<uses-permission android:name="android.permission.INTERNET"/>
<service
android:name=".NotificationMonitor"
android:label="nb"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NotificationMonitor extends NotificationListenerService {
@SuppressLint("NewApi")
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
//有新通知添加的时候调用
Log.e("onNotificationPosted","posted");
Log.i("xiaolong", "open" + "-----" + sbn.getPackageName());
Log.i("xiaolong", "open" + "------" + sbn.getNotification().tickerText);
Log.i("xiaolong", "open" + "-----" + sbn.getNotification().extras.get("android.title"));
Log.i("xiaolong", "open" + "-----" + sbn.getNotification().extras.get("android.text"));
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn) {
//通知被移除的时候调用
Log.e("onNotificationRemoved","posted");
Log.i("xiaolong", "remove" + "-----" + sbn.getPackageName());
}
private boolean isNotificationServiceEnable() {
return NotificationManagerCompat.getEnabledListenerPackages(this).contains(getPackageName()); }
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String string = Settings.Secure.getString(getContentResolver(),
"enabled_notification_listeners");
if (!string.contains(NotificationMonitor.class.getName())) {
startActivity(new Intent(
"android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
}
}
}
转载自blog.csdn.net/zzf0521/article/details/82896856