android - Is it possible to bind an activities to this service which works on background thread and always running? -
it's first question on so, hope question won't bad.
i have service, starts working when user launchs app , works until user kill via task killer or turn off device.
this service has background thread work data. need bind activities (from activities, not service) , (1-2 times per 30 seconds) send data binded activities.
structure of service:
public class myserv extends service { public static boolean started=false; public class workwithdata extends thread { @override public synchronized void start() { super.start(); //.. not important. } @override public void run() { if (running) return; while (true) { if(condition) mythread.sleep(30000); else { object data = recievemydata(); if (!data.isempty()) { //.. work recieved data, not important. senddatatobindedactivities(data); //this need. } mythread.sleep(10000); } } } } @override public void oncreate() { super.oncreate(); this.started=true; mythread = new workwithdata(); mythread.start(); } } well, found 1 question problem has little differences: don't need send data service, need send data binded activities (which service doesn't know @ all).
structure i'm looking for:
public class myact extends activity { @override public void oncreate(bundle bun) { super.oncreate(bun); if(!myserv.started) { intent service = new intent(getapplicationcontext(), myserv.class); getapplicationcontext().startservice(service); } bindtoservice(this); } @override public void onrecieveddata(object data) { //work recieved data service "myserv". } } i tried find solutions in android documentation didn't find need.
so, main question is: is possible work communications service activities?. if no: what should use purpose? if yes, just, sorry, can ask code or class names, because tried find , didn't...
thank you.
you need use remotecallbacklist
when clients bind service, need register them using remotecallbacklist.register().
when want send data bound clients, this:
int count = callbacklist.beginbroadcast(); (int = 0; < count; i++) { try { imyservicecallback client = callbacklist.getbroadcastitem(i); client.onrecieveddata(thedata); // here callback bound client's method // onrecieveddata() , pass "thedata" } catch (remoteexception e) { // can safely ignore exception. remotecallbacklist take care // of removing dead object us. } catch (exception e) { // not can here except log log.e("while calling remote client", e); } } callbacklist.finishbroadcast(); an example can found here kinda complicated, maybe don't need offers. in case, have look.
Comments
Post a Comment