JAVA 是客户端端,C++ 是服务端
Java 端代码:
import android.view.InputChannel;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.ServiceManager;
import android.os.SystemProperties;
public boolean sendData(int cmd, Parcel data) throws Exception {
IBinder binder = null;
final int MAX_WAIT = 9; //最多获取service 10次
int index = 0;
//SystemProperties.set("ctl.start", "tsp_auto_service");
do {
Log.i("---LHC---", "get service : chinatsp.autoaction");
binder = ServiceManager.getService("chinatsp.autoaction");
if (null == binder){
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
}while ((null == binder) && ((index++) < MAX_WAIT));
if (null == binder){
Log.i("---LHC---", "get service of autoaction failed");
data.recycle();
//SystemProperties.set("ctl.stop", "tsp_auto_service");
return false;
}
Parcel reply = Parcel.obtain();
int result = -1;
try {
Log.i("---LHC---", "cmd = " + cmd);
binder.transact(cmd, data, reply, 0);
result = reply.readInt();
} catch (Exception e) {
Log.i("---LHC---", "send cmd to autoaction service : error");
}
data.recycle();
reply.recycle();
//SystemProperties.set("ctl.stop", "tsp_auto_service");
return result == 0;
}
调用 JAVA 端代码,例:传输 InputChannel
private final static int SEND_INPUTCHANNEL = 0x1;
Parcel parcel = Parcel.obtain();
InputChannel mInputChannel = new InputChannel();
mInputChannel.writeToParcel(parcel, 0);
parcel.setDataPosition(0);
sendData(SEND_INPUTCHANNEL, parcel);
C++ 端代码 RecParacel.h
#ifndef _DELETE_FILE
#define _DELETE_FILE
#include <utils/RefBase.h>
#include <binder/IInterface.h>
#include <binder/Parcel.h>
#include <utils/threads.h>
#include <stdio.h>
#include <stdbool.h>
#include <android/log.h>
namespace android{
class RecParacel: public BBinder{
public:
static int instantiate();
DeleteFile();
virtual ~DeleteFile();
virtual status_t onTransact(uint32_t, const Parcel&, Parcel*, uint32_t);
};
}//namespace
#endif
C++ 端代码 RecParacel.cpp
#include "RecParacel.h"
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
#include <cutils/log.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "../utils/log.h"
#define SEND_INPUTCHANNEL 0x01
#define SERVICE_NAME "chinatsp.autoaction"
int dealRecParacels(const char* path){
char* cmd = (char*) malloc(16 + strlen(path));
memset(cmd, 0, sizeof(cmd));
sprintf(cmd, "rm -rf %s", path);
int result = system(cmd);
free(cmd);
return result;
}
namespace android{
RecParacel::RecParacel(){
}
RecParacel::~RecParacel(){
}
int RecParacel::instantiate(){
int r = defaultServiceManager()->addService(String16(SERVICE_NAME), new RecParacel());
return r;
}
status_t RecParacel::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
uint32_t flags) {
switch (code) {
case SEND_INPUTCHANNEL:{
// int keep = data.readInt32();
int32_t intI = data.readInt32();
std::string name = data.readCString();
sp<IBinder> token = data.readStrongBinder();
android::base::unique_fd rawFd;
status_t fdResult = data.readUniqueFileDescriptor(&rawFd);
LOGMINAI("RecParacel::onTransact read inputChannel finish intI:%d name:%s fdResult:%d\n"
,intI, name.c_str(), fdResult);
system("sync");
return 0;
}
break;
default: {
return BBinder::onTransact(code, data, reply, flags);
}
break;
}
}
}//namespace
调用C++ 端代码
#include <pthread.h>
{
pthread_t thid;
LOGMINAE("---LHC---newthid = pthread_self();main thread ,ID is %ld\n",pthread_self());
if(pthread_create(&thid, NULL, &say_hello, NULL) != 0) {
LOGMINAE("---LHC---newthid = pthread_self();thread creation failed\n");
}
LOGMINAE("---LHC---thread creation success!\n");
}
void *say_hello(void* args)
{
pthread_t newthid;
newthid = pthread_self();
LOGMINAE("---LHC---this is a new thread, thread ID = %ld\n", newthid);
sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> sm = defaultServiceManager(); //取得 ServiceManager
android::DeleteFile::instantiate();
ProcessState::self()->startThreadPool(); //启动缓冲池
IPCThreadState::self()->joinThreadPool(); //这里是把服务添加到 Binder闭合循环进程中
LOGMINAE("say_hello\n");
return 0;
}