service - Binder mechanisam to update UI in android -
in project using binder mechanism communicate remote service. remote service class call jni function , updates ui every 10 seconds. jni function has below code
static int n = 100; return ++n; this jni function calling in service class , updating ui below
public class myservice extends service{ private static final string tag = "myservice"; private final handler servicehandler = new handler(); @override public void oncreate() { // todo auto-generated method stub super.oncreate(); system.out.println("inside service oncreate"); log.d(tag, "entered onstart"); servicehandler.removecallbacks(sendupdatestoui); servicehandler.postdelayed(sendupdatestoui, 1000); // 1 second } 1. private imyservice.stub biostub = new imyservice.stub() { 2. 3. @override 4. public int intfromjni() throws remoteexception { 5. // todo auto-generated method stub 6. int libdata = abcd.intfromjni(); 7. system.out.println("inside ****** stub"+libdata); 8. return libdata; } }; @override public ibinder onbind(intent arg0) { // todo auto-generated method stub system.out.println("inside binder "); return biostub; } private runnable sendupdatestoui = new runnable() { public void run() { log.d(tag, "entered runnable"); try { biostub.intfromjni(); servicehandler.postdelayed(this, 10000); } catch (remoteexception e) { // todo auto-generated catch block e.printstacktrace(); } } }; } service class calling in activty @override protected void onresume() { // todo auto-generated method stub super.onresume(); intent serviceintent=new intent(); serviceintent.setclass(context, myservice.class); boolean ok=bindservice(serviceintent, mserviceconnection , context.bind_auto_create); log.v("ok", string.valueof(ok)); } private serviceconnection mserviceconnection=new serviceconnection(){ @override public void onserviceconnected(componentname arg0, ibinder service) { // todo auto-generated method stub system.out.println("inside serviceconnection"); myservice = imyservice.stub.asinterface(service); try { log.d("client", "entered mserviceconnection"); 8. int jnivalue = myservice.intfromjni(); 9. system.out.println("value if jni==>"+jnivalue); 10. } catch (remoteexception e) { // todo auto-generated catch block e.printstacktrace(); } } @override public void onservicedisconnected(componentname arg0) { // todo auto-generated method stub } }; here problem is, service class not updating ui every 10 seconds , value not incremeting in ui value incrementing every 10 seconds in service class i.e print statement below updating every 10 seconds in line number 7 in above code(service).
system.out.println("inside ****** stub"+libdata); but want update in ui also. i.e statement line num 10.
system.out.println("value if jni==>"+jnivalue); i`t not happening in code . how solve 1 .`
you missing way service tell activity new events. call activity service, intfromjni, one-off function call happens once - has no ongoing responsibility telling ui changes in future.
you should add listener class. example, add imyservicelistener.aidl this:
package com.something; interface imyservicelistener { void valueupdated(int newvalue); } and in imyservice.aidl add this:
import com.something.imyservicelistener; void addlistener(in imyservicelistener newlistener); then, within imyservice.stub class need implement addlistener - should add resulting object array of listeners, , call valueupdated on each listener, each time value changes.
within activity, should call myservice.addlistener , pass in object receives notification of changes. can display on ui within activity.
Comments
Post a Comment