简体   繁体   English

Android服务尚未绑定

[英]Android service not yet bound

I am trying to make a button that will change the interruption filter (None, Priority, All) in Android Lollipop. 我正在尝试创建一个按钮,以更改Android Lollipop中的中断过​​滤器(无,优先,全部)。 When I press the button, the log says Notification listener service not yet bound. 当我按下按钮时,日志显示Notification listener service not yet bound. . The service starts, so I guess I am trying to bind it wrong? 服务启动,所以我想我尝试将其绑定错误? I get the "NLS Started" log but not the "NLS Bound" one. 我收到"NLS Started"日志,但没有"NLS Bound"日志。 Here is my code: 这是我的代码:

MainActivity.java : MainActivity.java

public class MainActivity extends Activity {
    private NotificationService notifs;
    private ServiceConnection connection;

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

        notifs = new NotificationService();
        connection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                Log.d("NOTIF", "NLS Started");
                NotificationService.ServiceBinder binder = (NotificationService.ServiceBinder)service;
                notifs = binder.getService();
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                Log.d("NOTIF", "NLS Stopped");
            }
        };
        Intent intent = new Intent(this, NotificationService.class);
        startService(intent);
        bindService(intent, connection, Context.BIND_AUTO_CREATE);
        if(notifs.isBound()) {
            Log.d("NOTIFS", "NLS Bound");
        }

        final Button b = (Button) findViewById(R.id.b);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (notifs.getCurrentInterruptionFilter() == NotificationService.INTERRUPTION_FILTER_NONE) {
                    //set all
                    b.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_ring_volume));
                    notifs.requestInterruptionFilter(NotificationService.INTERRUPTION_FILTER_ALL);
                } else if (notifs.getCurrentInterruptionFilter() == NotificationListenerService.INTERRUPTION_FILTER_PRIORITY) {
                    //set none
                    b.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_ring_off));
                    notifs.requestInterruptionFilter(NotificationService.INTERRUPTION_FILTER_NONE);
                } else {
                    //set priority
                    b.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_ring_priority));
                    notifs.requestInterruptionFilter(NotificationService.INTERRUPTION_FILTER_PRIORITY);
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbindService(connection);
        connection = null;
    }
}

NotificationService.java : NotificationService.java

public class NotificationService extends NotificationListenerService {
    private final IBinder binder = new ServiceBinder();
    private boolean isBound = false;

    public NotificationService() {
    }

    public class ServiceBinder extends Binder {
        NotificationService getService() {
            return NotificationService.this;
        }
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startid) {
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("NOTIF", "Started");
        Toast.makeText(NotificationService.this, "NLS Started", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public boolean isBound() {
        return isBound;
    }
}

Service connection works with bindService() method only. 服务连接仅适用于bindService()方法。 Therefore, if you're getting "NLS Started" that means code from onServiceConnected() method of service connection has been executed which indicates that your service has been successfully bound with your respective activity. 因此,如果获取的是“ NLS Started”,则表示已执行服务连接的onServiceConnected()方法中的代码,表明您的服务已成功与各自的活动绑定。

As far as "NLS Bound" is concerned, sometimes service connection takes a second or two to bind the service with the activity. 就“ NLS绑定”而言,有时服务连接需要一到两秒钟才能将服务与活动绑定。 This delay will not stop the rest of your code from working ie the if statement below the bindService() method will get executed even before the service has been bound with your activity. 此延迟不会停止其余代码的工作,即,即使在服务与您的活动绑定之前,bindService()方法下面的if语句也将被执行。 For this reason we use the service connection. 因此,我们使用服务连接。 The service connection receives the service object when it is created and is also informed if the service is destroyed. 服务连接在创建时会接收到服务对象,并且还会通知服务是否被破坏。 See the following link for more details:- 有关更多详细信息,请参见以下链接:

http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent,android.content.ServiceConnection,int) http://developer.android.com/reference/android/content/Context.html#bindService(android.content.Intent,android.content.ServiceConnection,int)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM