xamarin vs2017 使用NotificationListenerService 做安卓拦截通知

1. 在MainActivity中设置

[Activity(Label = "DyHoldNotice", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {


            Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
            StartActivity(intent);

            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());

        }

    }

2.配置权限 AndroidManifest.xml


      
        
      
    

3. 监听消息类

[Service(Label ="ServiceName",Permission ="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE")]
    [IntentFilter(new[] {"android.service.notification.NotificationListenerService"})] 
    public class NLService : NotificationListenerService
    {
        public override void OnCreate()
        {
            base.OnCreate();
            Log.Info("监听服务启动", "Servico Criado");
        }
        public override void OnDestroy()
        {
            base.OnDestroy();
        }
        public override IBinder OnBind(Intent intent)
        {
            return base.OnBind(intent);
        }
        public override bool OnUnbind(Intent intent)
        {
            return base.OnUnbind(intent);
        }
        public override void OnNotificationPosted(StatusBarNotification sbn)
        {
            string packageName = sbn.PackageName;
            string content = sbn.Notification.TickerText.ToString();
            base.OnNotificationPosted(sbn);
        }

        public override void OnNotificationRemoved(StatusBarNotification sbn)
        {
            base.OnNotificationRemoved(sbn);
        }
    }

 

转载于:https://www.cnblogs.com/xyunp/p/10375470.html

你可能感兴趣的:(xamarin vs2017 使用NotificationListenerService 做安卓拦截通知)