【问题标题】:How to stop NotificationListenerService android?如何停止 NotificationListenerService android?
【发布时间】:2014-06-23 22:39:40
【问题描述】:

我在从kpbird's example 开发的Android 应用程序中使用了NotificationListenerService。即使应用程序被销毁,该服务也会在后台运行。有没有办法停止此服务并在应用启动时启动它?

【问题讨论】:

    标签: android service notifications listener


    【解决方案1】:

    NotificationListenerService 不能停止,因为我们启动服务系统后会调用 bindService() 。该服务会保持一个ServiceConnection,然后它不会响应stopService或stopSelf。

    作为我的搜索结果,我们也无法从服务实例中删除 ServiceConnection。

    【讨论】:

      【解决方案2】:

      在服务内创建一个广播接收器,它扩展了 NotificationListenerService 像这样

      public class Block_All_Notification2 extends NotificationListenerService {
      
      boolean check=false;
      
      CancelNotificationReceiver mReceiver = new CancelNotificationReceiver();
      public static final String Package_Name = "com.muhaiminurabir.notificationblocker";
      @Override
      public IBinder onBind(Intent intent) {
          return super.onBind(intent);
      }
      
      @Override
      public void onNotificationPosted(StatusBarNotification sbn){
          // Implement what you want here
          // Inform the notification manager about dismissal of all notifications.
          Log.d("Msg", "Notification arrived");
          start_blocking();
         /* if (false){
              cancelAllNotifications();
          }*/
          //Block_All_Notification2.this.cancelAllNotifications();
      }
      
      @Override
      public void onNotificationRemoved(StatusBarNotification sbn){
          // Implement what you want here
          Log.d("Msg", "Notification Removed");
      }
      
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          String block = intent.getStringExtra("block");
          Log.d("checking service 1",block+"");
          if (block.equals("yes")){
              check=true;
          }else if(block.equals("no")){
              check=false;
          }
          Log.d("checking service 1",check+"");
          return START_STICKY;
      
      }
      
      @Override
      public void onCreate() {
          super.onCreate();
          IntentFilter filter = new IntentFilter();
          filter.addAction(Package_Name);
          registerReceiver(mReceiver, filter);
      }
      
      @Override
      public void onDestroy() {
          super.onDestroy();
          unregisterReceiver(mReceiver);
      }
      
      public void start_blocking(){
          Log.d("checking service",check+"");
          if (check==true){
              cancelAllNotifications();
          }
      }
      
      class CancelNotificationReceiver extends BroadcastReceiver {
      
          @Override
          public void onReceive(Context context, Intent intent) {
              Log.d("received service","received");
              String action;
              if (intent != null && intent.getAction() != null) {
                  action = intent.getAction();
                  if (action.equals(Package_Name)) {
                      String block = intent.getStringExtra("block");
                      if (TextUtils.equals(block, "no")) {
                          Log.d("checking service 1",block+"");
                          if (block.equals("yes")){
                              check=true;
                              cancelAllNotifications();
                          }else if(block.equals("no")){
                              check=false;
                          }
                          Log.d("checking service 1",check+"");
                      } else if (TextUtils.equals(block, "yes")) {
                          Log.d("checking service 1",block+"");
                          if (block.equals("yes")){
                              check=true;
                              cancelAllNotifications();
                          }else if(block.equals("no")){
                              check=false;
                          }
                          Log.d("checking service 1",check+"");
                      }
                  }
              }
          }
      
      }
      
      }
      

      并在您的活动中添加此代码以与上面创建的服务进行通信

      Intent intent = new Intent();
              intent.setAction(Block_All_Notification2.Package_Name);
              intent.putExtra("block", "no");
              context.sendBroadcast(intent);
      

      【讨论】:

        【解决方案3】:

        NotificationListenerService 扩展了 Service,因此您可以通过在 Service 类中调用 stopSelf() 方法来停止服务。

        看看这个:NotificationListenerService 和这个Services

        希望对你有帮助,

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-13
          • 1970-01-01
          • 2012-03-16
          • 1970-01-01
          • 2013-07-25
          • 1970-01-01
          相关资源
          最近更新 更多