Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to communicate a service with an activity (broadcast, callbacks, etc)

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();

        }

    };
like image 763
Pipeline Avatar asked Jul 18 '13 11:07

Pipeline


People also ask

How do you share data between activity and service?

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.

How do I send a callback from service to activity?

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 .

How to send message from service to activity in android?

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."


1 Answers

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.

like image 103
galex Avatar answered Oct 26 '22 11:10

galex