android binder - 客户端(java层) 调用 服务端(c++层) 例子

本文介绍了一个使用Android作为客户端与C++服务端进行跨进程通信的例子。客户端通过Binder机制调用服务端提供的功能,并展示了如何在Android.mk中配置服务端编译选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

客户端是Android程序和服务端采用C++程序编写


客户端新建一个Android程序,在包名 com.example.client3下有文件,如下
MainActivity.java
----------------------------------------------------------------------------------------------------------
package com.example.client3;

import android.app.Activity;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcel;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;

public class MainActivity extends Activity {
     private static final String TAG = MainActivity.class.getSimpleName();
     private static final java.lang.String DESCRIPTOR = "sample.hello";
     private static final int FUNC_CALLFUNCTION = 1;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          Log.i(TAG, "Client oncreate ");
          Parcel _data = Parcel.obtain();
          Parcel _reply = Parcel.obtain();
          IBinder b = ServiceManager.getService(DESCRIPTOR);
           try {
              _data.writeInterfaceToken(DESCRIPTOR);
              b.transact(FUNC_CALLFUNCTION, _data, _reply, 0);
              _reply.readException();
              _reply.readInt();
          } catch (RemoteException e) {
               // TODO Auto-generated catch block
              e.printStackTrace();

          } finally {
              _reply.recycle();
              _data.recycle();
          }
     }
}


sampleService.cpp
----------------------------------------------------------------------------------------------------------
#include <binder/IServiceManager.h>
#include <binder/IBinder.h>
#include <binder/Parcel.h>
#include <binder/ProcessState.h>
#include <binder/IPCThreadState.h>
using namespace android;
#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "sampleService"
#define SAMPLE_SERIVCE_DES "sample.hello"
#define FUNC_CALLFUNCTION 1
class SampleService: public BBinder {
public:
     SampleService() {
          mydescriptor = String16(SAMPLE_SERIVCE_DES);
     }
     
     virtual ~SampleService() {
     }

     virtual const String16& getInterfaceDescriptor() const {
           return mydescriptor;
     }
     
protected:
     
     void callFunction() {
          LOGE( "Service callFunction-----------");
     }
     
     virtual status_t onTransact(uint32_t code, const Parcel& data,
              Parcel* reply, uint32_t flags = 0) {
          LOGD( "Service onTransact, code = %d" , code);
           switch (code) {
           case FUNC_CALLFUNCTION:
              callFunction();
               break;
           default:
               return BBinder::onTransact(code, data, reply, flags);
          }
           return 0;
     }
private:
     String16 mydescriptor;
};

int main() {
     sp < IServiceManager > sm = defaultServiceManager();
     SampleService* samServ = new SampleService();
     status_t ret = sm->addService(String16(SAMPLE_SERIVCE_DES), samServ);
     LOGD("Service main addservice ");
     ProcessState::self()->startThreadPool();
     IPCThreadState::self()->joinThreadPool( true);
     return 0;
}


Android.mk
-------------------------------------------------------------------
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := eng

LOCAL_SRC_FILES:= \
           sampleService.cpp

LOCAL_C_INCLUDES += \
        $(LOCAL_PATH) \

LOCAL_SHARED_LIBRARIES : = \
    libcutils \
    libbinder \
    libutils \
    libhardware

LOCAL_CFLAGS := -DRIL_SHLIB

LOCAL_MODULE:= sampleService

include $(BUILD_EXECUTABLE)

-------------------------------------------------------------------
1、先编译好服务,然后将服务跑起来
2、运行android程序,抓log



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值