推送拦截

<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