跨进程通过引用返回AIDL接口实现

问题描述

| 我正在进行一个小Android项目,其中涉及一些IPC,其中客户端活动绑定到我的服务。 我正在将AIDL用于IPC和RPC,效果很好,但是我无法将服务端实例化的AIDL接口实现返回给客户端: 当客户端与服务在同一进程中运行时(意味着在本地运行服务),一切正常。 但是,当客户端和服务在不同的进程中分开时,在ILogDroidBinder.aidl中定义的startLogSession方法始终返回null。 此接口中实现的另一种方法getSessionIds始终有效(本地和跨进程),该方法返回包含int的List。 我正在大胆猜测,并假设我的ILogDroidSession实现也应该实现Parcelable,但这不会起作用,因为我无法打包包含对sqliteDatabase的引用的对象(或者可以吗?)。 这是相关的代码。 如果有人可以帮助我,我真的很高兴。也许我只是在某个地方遗漏了一点,因为这是我的第一个Android项目,并且我还没有参与其中。 ILogDroidSession.aidl(这是我想返回给客户端的实现):
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);
}
ILogDroidBinder.aidl(传递到客户端的onServiceConnected的IBinder接口):
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();
}
LogDroidService.java(来自我的服务的相关代码):
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<Integer> 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<Integer> getSessionIds() {
    return database.getSessionIds();
  }
}
MainActivity.java(相关客户端代码):
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<Integer> 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) {
    }
  };
}
    

解决方法

        ILogDroidSession可以被定义为java文件中的接口,而不应该位于AIDL中。 如果客户端和LogDroidService在不同的进程中运行,则LogDroidSession应该是可打包的以通过IPC发送/接收。 跨进程交换的数据应该只是发送者和接收者都可以通过协议理解的字节流。
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?).
LogDroidSession无法在此处打包,请向ILogDroidBinder添加新功能,以返回与会话相关的信息(以纯数据类型的形式)。     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...