我想在C++中創建一個綁定器服務,並且能夠綁定到來自java的 服務。我的問題是如何?我已經模擬了框架/ base/camera/tests/CameraServiceTest/ CameraServiceTest.cpp中的 實現。它編譯很好,沒有任何警告,我可以運行它。但我如何連接到它?如何連接到java中的binder C++服務?
該服務的代碼是在這裏:
/* Imitating frameworks/base/camera/tests/CameraServiceTest/
CameraServiceTest.cpp */
#include <binder/IInterface.h>
#include <binder/IPCThreadState.h>
#include <binder/IServiceManager.h>
#include <binder/ProcessState.h>
#include <binder/IBinder.h>
using namespace android;
class IPokeService : public IInterface {
protected:
enum {
POKE = IBinder::FIRST_CALL_TRANSACTION
};
public:
DECLARE_META_INTERFACE(PokeService);
virtual void poke() = 0;
};
class BnPokeService : public BnInterface<IPokeService> {
virtual status_t onTransact(uint32_t code, const Parcel& data,
Parcel* reply, uint32_t flags = 0);
};
class BpPokeService : public BpInterface<IPokeService> {
public:
BpPokeService(const sp<IBinder>& impl) :
BpInterface<IPokeService>(impl) {
}
virtual void poke() {
Parcel data, reply;
remote()->transact(POKE, data, &reply);
}
};
IMPLEMENT_META_INTERFACE(PokeService, "PokeService");
status_t BnPokeService::onTransact(uint32_t code, const Parcel& data,
Parcel* reply, uint32_t flags) {
switch(code) {
case POKE: {
poke();
return NO_ERROR;
} break;
default:
return BBinder::onTransact(code, data, reply, flags);
}
}
class PokeService : public BnPokeService {
virtual void poke() {
printf("Poked\n");
}
};
int main(int argc, char **argv)
{
defaultServiceManager()->addService(String16("PokeService"), new
PokeService());
ProcessState::self()->startThreadPool();
android::ProcessState::self()->startThreadPool();
LOGI("Poke service is now ready");
IPCThreadState::self()->joinThreadPool();
return 0;
}
這裏是我的Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := main.cpp
LOCAL_CFLAGS += -DPLATFORM_ANDROID
LOCAL_MODULE := poke
# for now, until I do a full rebuild.
LOCAL_PRELINK_MODULE := false
LOCAL_SHARED_LIBRARIES += liblog
LOCAL_SHARED_LIBRARIES += libutils libui libcutils
LOCAL_SHARED_LIBRARIES += libbinder
include $(BUILD_EXECUTABLE)
該服務不會出現,當我在用java做的列表:
Context context = getBaseContext();
ActivityManager am = (ActivityManager)
context.getSystemService(Activity.ACTIVITY_SERVICE);
List<RunningServiceInfo> serviceInfos = am.getRunningServices(50);
for (RunningServiceInfo service : serviceInfos) {
Log.i("Service", "Process " + service.process + " with component " + service.service.getClassName());
}
這隻會給我從logcat:
I/Service ( 715): Process system with component
com.android.internal.service.wallpaper.ImageWallpaper
I/Service ( 715): Process com.android.phone with component
com.android.phone.BluetoothHeadsetService
I/Service ( 715): Process com.android.inputmethod.latin with
component com.android.inputmethod.latin.LatinIME
任何幫助,將不勝感激!
親切的問候, 塞繆爾