【问题标题】:How can an Activity access a method of NotificationListenerService?Activity 如何访问 NotificationListenerService 的方法?
【发布时间】:2015-05-24 01:30:05
【问题描述】:

我有一个活动类,需要获取设备当前的中断过滤器设置。

因此我有一个MyNotificationListenerService 类,它派生自 NotificationListenerService 并实现 onInterruptionFilterChanged()。

但是,onInterruptionFilterChanged() 仅在中断过滤器更改时才被调用。当我的应用程序启动时,我需要找出中断过滤器的当前值是多少。 NotificationListenerService 有一个方法是getCurrentInterruptionFilter()

我的问题是:当我的应用启动时,MyActivity 如何调用MyNotificationListenerServicegetCurrentInterruptionFilter()

操作系统自动创建并启动MyNotificationListenerService,是否有某种方式MyActivity 可以获取该对象的句柄以便显式调用getCurrentInterruptionFilter()? 如果不是,那么应该有什么通信机制让MyActivity 能够从MyNotificationListenerService 获得initial 中断设置?

.

【问题讨论】:

  • MyNotificationListenerService类创建一个对象而不是访问方法怎么样?
  • @Prera​​k。这正是我的问题所在。 MyActivity 如何访问 MyNotificationListenerService 的方法?正如我在第 5 段中所说的那样 - 操作系统创建了一个 MyNotificationListenerService 对象,因此 MyActivity 如何获取该对象是我的问题所在。

标签: android android-activity android-service


【解决方案1】:

您想从 Activity 绑定到服务。 Android 文档在http://developer.android.com/guide/components/bound-services.html 有对此的详细解释

这是一个如何工作的示例。

您的活动:

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;
        }
    }
}

您的服务:

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;
        }
    }
}

【讨论】:

  • 创建活动时出现异常,有一条日志消息表明连接为空。我在某处(现在找不到链接)读到 NotificationListenerService 是“特殊的”,因为只有操作系统可以绑定到它。
  • 我已经更新了答案,我遗漏了负责空连接的mServiceConnection = new MyServiceConnection();。测试了一下,可以成功绑定Service
  • 感谢您的回答。但是我发现添加此代码似乎会阻止 NotificationListenerService 实际工作。如果应用程序运行时没有上面的代码,NLS 不会做任何事情,直到用户授予应用程序的通知访问权限,一旦他们这样做,就会调用 NLS 的 onListenerConnected() 并且对中断过滤器的更改会导致 onInterruptionFilterChanged() 得到调用等。但是,如果添加了上述代码,那么当用户授予通知访问权限时,onInterruptionFilterChanged() 不会被调用,也不会被调用。
猜你喜欢
  • 2011-10-31
  • 1970-01-01
  • 2013-07-25
  • 2013-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多