'How to stop NotificationListenerService android?

I'm using a NotificationListenerService in my android app that was developed from kpbird's example. The service runs in the background even after the app is destroyed. Is there a way to stop this service and start it when app is started?



Solution 1:[1]

NotificationListenerService can not be stopped, because after we start the service system will call bindService() . The service will keep a ServiceConnection, then it will not response to the stopService or stopSelf.

As my search result , we also can not remove ServiceConnection from a Service instance.

Solution 2:[2]

Create a broadcast receiver inside service which extends NotificationListenerService like this

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+"");
                }
            }
        }
    }

}

}

and in your activity add this code to communicate with service which create above

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

Solution 3:[3]

Perhaps this problem has been solved.

However, I recently found a way to start or stop NotificationListenerService, and I would like to share it.

Of course, this is not the official method I think.

Therefore, I think you can just look at it for reference that there is also this way.

I got a hint from the fact that in order to run NotificationListenerService, users must be authorized manually.

When the Start Button is pressed, the NotificationListener permission window is displayed, and a Toast Message is displayed to guide the user to request permission.

On the contrary, when the Stop Button is pressed, the NotificationListener permission window is displayed, and at the same time, a Toast Message is displayed to guide the user to revoke permission.

My code is like belows.

main_start_service_btn.setOnClickListener {
    if(isNotificationPermissionAllowed()) {
        Toast.makeText(this, "Already Starting Service.", Toast.LENGTH_LONG).show()
    }
    else {
        Toast.makeText(this, "Please allow permission in the next window", Toast.LENGTH_LONG).show()
                                                            
startActivity(Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"))
    }
}

main_stop_service_btn.setOnClickListener {
    if(isNotificationPermissionAllowed()) {
        Toast.makeText(this, "Please deny permission in the next window", Toast.LENGTH_LONG).show()
                startActivity(Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"))
    }
    else {
        Toast.makeText(this, "Already Stopped Service.", Toast.LENGTH_LONG).show()
    }
}

Solution 4:[4]

The NotificationListenerService extends Service so yes you can stop a service by calling stopSelf() method in Service class.

Look at this: NotificationListenerService and this Services

Hope it Helps,

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fantasy_RQG
Solution 2 Muhaiminur Rahman
Solution 3 Jun
Solution 4 bean_droid