17

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

        }

    };
Cœur
  • 37,241
  • 25
  • 195
  • 267
Pipeline
  • 567
  • 1
  • 8
  • 23
  • 1
    What problems do you anticipate with broadcasts? – Jeremy Roman Jul 22 '13 at 12:57
  • Use the same library for different apps: Same broadcast action name on the library with different apps: I don't know exactly what is going to happen. Anyway, do you think this is a good approach? – Pipeline Jul 22 '13 at 16:11
  • What is the problem behind receivers.. receivers are best choice. Are all lib and projects developed by you? – Pankaj Kumar Jul 26 '13 at 08:16
  • Well, as I answered, there is no problem with receivers, the problem is more about the integration in the future with other projects. Thanks for your help :) – Pipeline Jul 26 '13 at 09:18

2 Answers2

6

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.

galex
  • 3,279
  • 2
  • 34
  • 46
  • The service is running on their own process, but I can modify it. Thanks for the answer. – Pipeline Jul 22 '13 at 16:09
  • About the strings.xml, what happen if a liberate the library as a *.jar, you can't change the strings.xml. I'm looking for a generic solution. – Pipeline Jul 23 '13 at 08:33
  • You can deliver the code part in a jar inside a Library Project, side by side with the resources (strings.xml) (that's how Google Play Services are delivered to us btw) http://developer.android.com/tools/projects/index.html#LibraryProjects – galex Jul 23 '13 at 12:13
1

Well, finally I will use a Callbacks implementation. The architecture is going to be like this:

App

  • Main Activity (Connect with the LibService)
  • LibService (is going to have the callbacks and broadcast receivers)

Library

  • Callbacks and interfaces but not running in a service.

This is the best approach to me to the future integration on other projects, and the library is going to be more simple without aidl and services.

I thought the use of the receivers was a very good options, but thinking about the integration on other projects this is the best way to do it (for me).

Pipeline
  • 567
  • 1
  • 8
  • 23