What I have:
I have a library running on a process using aidl. I have an app that uses this library, and on the messaging activity, I connect with the service to send messaging and I have a broadcast receiver to manage the incoming messages.
The problem?
if this library are going to be used by two apps on the same device the broadcast actions are going to be the same, and I will have problems when I send a broadcast.
What is my doubt?
What is the best way to "listen" the new incoming messages that I receive on my library and send it to the App. Maybe a callback? or there are any better solution?
More information
The library provides a few methods to start a session, and other methods for send different type of messages (images, text, locations, etc...) and I receive a callback from another library, that uses C and C++, when a new message is incoming.
If you need more information feel free to ask.
My Code:
IRemote.aidl
interface IRemote
{
int sendTextMessage(String to, String message);
}
WrapperLibrary.java
public class MyLibrary extends Service {
// Current context, used to sendbroadcast() from @Callbacks
private Context mContext = this;
private static MyLibrary instance = new MyLibrary();
//Executor to start a new thread from the service.
final ExecutorService service;
@Override
public IBinder onBind(Intent arg0) {
//Return the interface.
return mBinder;
}
/** Return the 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 i = -1;
Future<Integer> task;
task = service.submit(new Callable<Integer>() {
public Integer call() {
return tu.tu_message_send_text(to, message);
}
});
try {
i = 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:" + from + " with message: " + message);
Intent i = new Intent(BroadcastActions.INCOMING_TEXT_MESSAGE);
i.putExtra("from", from);
i.putExtra("message", message);
mContext.sendBroadcast(i);
}
}
MainActivity.java On this activity I have a broadcast receiver and I can update the UI with a new message
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extra = 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();
}
};
Primitive Data Types To share primitive data between Activities/Services in an application, use Intent. putExtras(). For passing primitive data that needs to persist use the Preferences storage mechanism. The android.
You need to create a BroadcastReceiver in your activity (be sure to register it in onResume() and unregister it in onPause() ) and notify it via a broadcast, providing an Intent .
the mission : "Handle the Message in GeolocationService, and use the 'replyTo' property to send a new Message back to MainActivity with the latitude in 'arg1' and the longitude in 'arg2'. Then call setCoordinates(int lat, int long) from within MainActivity to update the coordinates."
I was going to suggest to use LocalBroadcastManager or if it becomes messy EventBus, but if the service runs in its own process (which is not something I'm sure about) the messaging will not be passed from one process to the other.
So I would suggest to define the Broadcast Action from the service in strings.xml and make it different for every app. Of course, you'll have to be careful as you'll need also to update the receiver action for every app.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With