Josip Maričević
Josip Maričević

Reputation: 417

Access NotificationListenerService class from main activity and vice versa

I need to make a connection between NotificationListenerService class and the MainActivity class. So far service was working independently from the activity and just logged new services. Now I need to connect then to do something with data from the service. More precisely I need to access the getActiveNotification method from the main activity to get notifications and do something with them. And after onNotificationPosted catches notification, add it to recycler view on the main activity. A saw some people do it with a service binder, but can't find how to do that. Both classes are almost empty so I think there is no need to post them.

I made the connection to the service, how could I now call the addNotificationsToList method from the NotificationListener service??

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerViewAdapter adapter;
    private NotificationListener notificationListenerService;
    private boolean bound = false;
    private static final String tag = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = new Intent(this, NotificationListener.class);
        bindService(intent, connection, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unbindService(connection);
        bound = false;
    }

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            NotificationListener.LocalBinder binder = (NotificationListener.LocalBinder) service;
            notificationListenerService = binder.getService();
            bound = true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            bound = false;
        }
    };

    private void init(){
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        adapter = new RecyclerViewAdapter();
        recyclerView.setAdapter(adapter);

        final Button button = findViewById(R.id.button_id);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.i(tag, String.valueOf(notificationListenerService.getNum()));
            }
        });
    }

    public void addNotificationsToList(StatusBarNotification sbn){
        //Add to recyclerView
    }
}
public class NotificationListener extends NotificationListenerService {

    private final IBinder binder = new LocalBinder();

    class LocalBinder extends Binder{
        NotificationListener getService(){
            return NotificationListener.this;
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

    @Override
    public StatusBarNotification[] getActiveNotifications() {
        return super.getActiveNotifications();
    }

    public int getNum(){
        return 2;
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
    }

}

Upvotes: 4

Views: 819

Answers (1)

David Wasser
David Wasser

Reputation: 95588

Read up about bound services. Your Activity can bind to the Service and then it can call methods on the Service that you define using AIDL. In this way the Activity can get data from the Service.

Upvotes: 1

Related Questions