gpt4 book ai didi

android - 跨进程通过引用返回一个AIDL接口(interface)实现

转载 作者:行者123 更新时间:2023-11-29 02:12:55 26 4
gpt4 key购买 nike

我正在进行一个小的 Android 项目,其中涉及一些 IPC,其中客户端 Activity 绑定(bind)到我的服务。
我将 AIDL 用于 IPC 和 RPC,效果很好,但我无法将服务端实例化的 AIDL 接口(interface)实现返回给客户端:

当客户端与服务在同一进程中运行时——意味着在本地运行服务——一切正常。
但是当客户端和服务在不同的进程中分离时,在 ILogDroidBinder.aidl 中定义的方法 startLogSession 总是返回 null。
在此接口(interface)中实现的另一个方法 -- getSessionIds -- 返回一个包含整数的列表,始终有效(本地和跨进程)。

我大胆猜测并假设我的 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接口(interface)):

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(来 self 的服务的相关代码):

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文件中的接口(interface),它不应该在AIDL中。

如果客户端和 LogDroidService 在不同的进程中运行,LogDroidSession 应该可以通过 IPC 进行发送/接收。

跨进程交换的数据应该只是发送方和接收方都通过协议(protocol)理解的字节流。

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 添加新的函数返回 session 相关信息(以普通数据类型的形式)。

关于android - 跨进程通过引用返回一个AIDL接口(interface)实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6198534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com