android - Best way to communicate a service with an activity (broadcast, callbacks, etc) -
what have:
i have library running on process using aidl. have app uses library, , on messaging activity, connect service send messaging , have broadcast receiver manage incoming messages.
the problem?
if library going used 2 apps on same device broadcast actions going same, , have problems when send broadcast.
what doubt?
what best way "listen" new incoming messages receive on library , send app. maybe callback? or there better solution?
more information
the library provides few methods start session, , other methods send different type of messages (images, text, locations, etc...) , receive callback library, uses c , c++, when new message incoming.
if need more information feel free ask.
my code:
iremote.aidl
interface iremote { int sendtextmessage(string to, string message); }
wrapperlibrary.java
public class mylibrary extends service { // current context, used sendbroadcast() @callbacks private context mcontext = this; private static mylibrary instance = new mylibrary(); //executor start new thread service. final executorservice service; @override public ibinder onbind(intent arg0) { //return interface. return mbinder; } /** return current instance */ public static wrapperlibrary getinstance() { return instance; } private final iremote.stub mbinder = new iremote.stub() { @override public int sendtextmessage(string to, string message) throws remoteexception { log.d(tag, "send text message. "); int = -1; future<integer> task; task = service.submit(new callable<integer>() { public integer call() { return tu.tu_message_send_text(to, message); } }); try { = task.get(); } catch (exception e) { log.e(tag, "send text message: exception *** " + e.getmessage()); } log.d(tag, "send text message: status code: " + i); return 0; } }
callbacks.java
public class callbacks extends jnicallback { private context mcontext; public callbacks(context context) { this.mcontext = context; } public void on_incoming_text_message(final string from, final string message) { log.d(tag, " incoming text message from:" + + " message: " + message); intent = new intent(broadcastactions.incoming_text_message); i.putextra("from", from); i.putextra("message", message); mcontext.sendbroadcast(i); } }
mainactivity.java on activity have broadcast receiver , can update ui new message
public class messagereceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { bundle = intent.getextras(); string incomingmessage = ""; if(extra != null) { incomingmessage = extra.getstring("message"); addnewmessage(new message(incomingmessage, false)); } toast.maketext(messagingactivity.this, "incoming message", toast.length_long).show(); } };
i going suggest use localbroadcastmanager or if becomes messy eventbus, if service runs in own process (which not i'm sure about) messaging not passed 1 process other.
so suggest define broadcast action service in strings.xml , make different every app. of course, you'll have careful you'll need update receiver action every app.
Comments
Post a Comment