Note that there are some explanatory texts on larger screens.

plurals
  1. POReturning an AIDL interface implementation by reference across processes
    primarykey
    data
    text
    <p>I have a little Android project going on which involves some IPC where client Activities bind to my service.<br/> I'm using AIDL for IPC and RPC which works pretty good, but I'm having trouble returning a service-side instantiated AIDL interface implementation to the clients:</p> <p>When the client is running in the same process as the service -- meaning running the service locally -- everything works just fine.<br/> But when client and service are seperated in different processes the method startLogSession, which is defined in ILogDroidBinder.aidl always returns null.<br/> The other method implemented in this interface -- getSessionIds -- which returns a List containing ints, always works (locally and cross-process).</p> <p>I'm taking a wild guess and suppose my ILogDroidSession implementation should also implement Parcelable, but that wouldn't work, because I can't parcel an object containg a reference to an SQLiteDatabase (or can I?).</p> <p>Here is the relevant code. I'd really be glad if someone could help me out here. Maybe I'm just missing a point somewhere, since this is my first Android project and I'm not quite involved yet.</p> <p>ILogDroidSession.aidl (An implementation of this is what I want to return to the client):</p> <pre class="lang-java prettyprint-override"><code>package net.sourceforge.projects.logdroid; interface ILogDroidSession { /** * Logs the given text to the error message channel of the current logging * session. * @param text Text to log. */ void logError(in String text); } </code></pre> <p>ILogDroidBinder.aidl (The IBinder interface passed to the client's onServiceConnected):</p> <pre class="lang-java prettyprint-override"><code>package net.sourceforge.projects.logdroid; import net.sourceforge.projects.logdroid.ILogDroidSession; interface ILogDroidBinder { /** * Starts a new LogDroid session which handles all logging events. * @param sessionName The name of the session. * @return An instance of ILogDroidSession. */ ILogDroidSession startLogSession(in String sessionName); /** * Gets a list with all available LogSession ids. */ List getSessionIds(); } </code></pre> <p>LogDroidService.java (Relevant code from my service):</p> <pre class="lang-java prettyprint-override"><code>public class LogDroidService extends Service { /** * The binder interface needed for Activities to bind to the * {@code LogDroidService}. */ private final ILogDroidBinder.Stub binder = new ILogDroidBinder.Stub() { /** * Starts a new LogDroidSession. */ public ILogDroidSession startLogSession(String sessionName) { return LogDroidService.this.createSession(sessionName); } /** * Gets all available session ids. */ public List&lt;Integer&gt; getSessionIds() { return LogDroidService.this.getSessionIds(); } }; /** * The database connection to be used for storing and retrieving log entries. */ private LogDroidDb database; @Override public void onCreate() { super.onCreate(); database = new LogDroidDb(getApplicationContext()); try { database.open(); // opens as writable database } catch ( SQLException ignorefornow ) { } } @Override public IBinder onBind(Intent ignore) { return binder; } /** * Creates a new LogDroidSession which will be returned to the user as a * AIDL remote object. * @param sessionName Name of the session. * @return A new instance of ILogDroidSession */ ILogDroidSession createSession(String sessionName) { LogDroidSession session = new LogDroidSession(database, sessionName); session.addLoggingOccurredListener(this); return session; } /** * Retrieves all session ids. * @return Array containing all LogDroidSession ids. */ ArrayList&lt;Integer&gt; getSessionIds() { return database.getSessionIds(); } } </code></pre> <p>MainActivity.java (Relevant client code):</p> <pre class="lang-java prettyprint-override"><code>public class MainActivity extends Activity { private ILogDroidSession session; private ILogDroidBinder binder; private ServiceConnection con = new ServiceConnection() { public void onServiceConnected(ComponentName arg0, IBinder arg1) { binder = ILogDroidBinder.Stub.asInterface(arg1); // always works try { // works locally but always returns null when cross-process session = binder.startLogSession("TestSession"); // always works List&lt;Integer&gt; ids = binder.getSessionIds(); } catch ( Exception ex) { // no exceptions are thrown either when running locally or cross-process Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_LONG).show(); } } public void onServiceDisconnected(ComponentName arg0) { } }; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload