asdafas
asdafas

Reputation: 51

start and stop the service of notificationlistener

How can I start and stop the service of NotificationListener for Android?

I want to start the service at a specific time and also want to stop it at a specific time.

How is it possible?

(I'm asking about the NotificationListener library.)

Upvotes: 2

Views: 1819

Answers (2)

David784
David784

Reputation: 7464

NotificationListenerService is a special kind of service, and it is started/stopped by the system, when it is selected/deselected in the "Notification access" settings screen. You can create a "shortcut" to this method doing something like this:

Intent intentS = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
startActivity(intentS);

Also, if you are doing development work, you want to make sure you uncheck notification access in the settings BEFORE deploying a new version to the phone. In my experience, if you don't, you'll need to reboot your phone before your service will start working again.

Upvotes: 0

Bö macht Blau
Bö macht Blau

Reputation: 13009

As the NotificationListenerService is a kind of Service, you are able to start or stop it like any other Service, e.g.

you first extend NotificationListenerService

public class NotificationListener extends NotificationListenerService {...}

and implement the callbacks you need. Please make sure to edit your Manifest as described here

Now you can start your Service by calling

startService(new Intent(com.example.servicedemo.NotificationListener));

(where of course you have to substitute com.example... by your own package name :) )

For stopping just call

stopService(new Intent(...));

Upvotes: 1

Related Questions