How can I access the NotificationListenerService method?

I have an activity class that should get the interrupt filter setting of the current device.

Therefore, I have a class MyNotificationListenerService that comes from NotificationListenerService and implements onInterruptionFilterChanged ().

However, onInterruptionFilterChanged() is called only when the interrupt filter changes. When my application starts, I need to find out what the current value of the interrupt filter is. NotificationListenerService has a method for this getCurrentInterruptionFilter() .

My question is: how MyActivity call MyNotificationListenerService getCurrentInterruptionFilter() when the application starts?

The OS automatically creates and runs MyNotificationListenerService , can MyActivity ever get a handle to this object to explicitly call getCurrentInterruptionFilter() ? If not, what communication mechanism should be in order for MyActivity to get the initial interrupt setting from MyNotificationListenerService ?

.

+6
source share
1 answer

You want to tie the service to your business. Android docs have a detailed explanation of this at http://developer.android.com/guide/components/bound-services.html

Here is an example of how this will work.

Your activity:

 public class MyActivity extends Activity { private MyNotificationListenerService mService; private MyServiceConnection mServiceConnection; ... protected void onStart() { super.onStart(); Intent serviceIntent = new Intent(this, MyNotificationListenerService.class); mServiceConnection = new MyServiceConnection(); bindService(serviceIntent, mServiceConnection, BIND_AUTO_CREATE); } protected void onStop() { super.onStop(); unbindService(mServiceConnection); } private class MyServiceConnection implements ServiceConnection { @Override public void onServiceConnected(ComponentName name, IBinder binder) { mService = ((MyNotificationListenerService.NotificationBinder)binder).getService(); } @Override public void onServiceDisconnected(ComponentName name) { mService = null; } } } 

Your service:

 public class MyNotificationListenerService extends NotificationListenerService { ... private NotificationBinder mBinder = new NotificationBinder(); @Override public IBinder onBind(Intent intent) { return mBinder; } public class NotificationBinder extends Binder { public MyNotificationListenerService getService() { return MyNotificationListenerService.this; } } } 
0
source

Source: https://habr.com/ru/post/984052/


All Articles