Android: service 'not yet bound' but onBind() is called? -
i trying make button change interruption filter (none, priority, all) in android lollipop. when press button, log says w/notificationlistenerservice[notificationservice]: notification listener service not yet bound. , not change interruption filter. "nls started" , "nls bound" logs. why giving me warning? here code:
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); 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 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:
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; log.d("notif", "nls bound"); 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; } }
replace onbind() method in service this:
@override public ibinder onbind(intent intent) { isbound = true; string action = intent.getaction(); log.d("notif", "onbind: " + action); if (service_interface.equals(action)) { log.d("notif", "bound system"); return super.onbind(intent); } else { log.d("notif", "bound application"); return binder; } } also check service declared in manifest shown in overview of documentation.
if service correctly declared in manifest, , notification access enabled in security / sound & notification, system bind service using action notificationlistenerservice.service_interface. bind request, service must return binder notificationlistenerservice, super.onbind(intent).
Comments
Post a Comment