从主要活动访问NotificationListenerService类,反之亦然

问题描述

我需要在NotificationListenerService类和MainActivity类之间建立连接。到目前为止,服务与活动无关,只是记录了新服务。现在,我需要连接,然后对服务中的数据进行处理。更准确地说,我需要从主活动访问getActiveNotification方法获取通知并对其进行操作。在onNotificationPosted捕获通知后,将其添加到主活动的回收者视图中。有人看到有人用服务绑定器来做,但是找不到如何做。 这两个课程都几乎是空的,所以我认为没有必要发布它们。

我已与服务建立连接,现在如何从addNotificationsToList服务中调用NotificationListener方法呢?

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) {
    }

}

解决方法

阅读有关绑定服务的信息。您的Activity可以绑定到Service,然后可以在您使用AIDL定义的Service上调用方法。这样Activity可以从Service获取数据。