如何定制自己的native service: example (C++)

来源:互联网 发布:知我网是正品吗 编辑:程序博客网 时间:2024/06/09 15:42

IMyService.h:

#ifndef MY_SERVICE_DEMO

#define MY_SERVICE_DEMO

  

#include <stdio.h>

#include <binder/IInterface.h>

#include <binder/Parcel.h>

#include <binder/IBinder.h>

#include <binder/ProcessState.h>

#include <binder/IPCThreadState.h>

#include <binder/IServiceManager.h>

 

using namespace android;

namespace android

{

   class IMyService: public IInterface {

       public:

           DECLARE_META_INTERFACE(MyService);

           virtual void sayHello() = 0;

   };

 

   enum {

       HELLO = IBinder::FIRST_CALL_TRANSACTION,

   };

 

   class BpMyService: public BpInterface<IMyService> {

       public:

           BpMyService(const sp<IBinder>& impl);

           virtual void sayHello();

   };

 

   class BnMyService: public BnInterface<IMyService> {

       public:

           virtual status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply,

                   uint32_t flags = 0);

           virtual void sayHello();

   };

 

}

 

#endif


IMyService.cpp:

#include "IMyService.h"

 

namespace android

{

   IMPLEMENT_META_INTERFACE(MyService, "android.demo.IMyService");

 

   // client

   BpMyService::BpMyService(const sp<IBinder>& impl):BpInterface<IMyService>(impl)

   {

   }

 

   // to implement client's sayhello method

   void BpMyService::sayHello() {

       printf("BpMyService::SayHello\n");

       Parcel data, reply;

       data.writeInterfaceToken(IMyService::getInterfaceDescriptor());

       remote()->transact(HELLO, data, &reply);

       printf("get num from BnService: %d\n", reply.readInt32());

   }

 

   //Server side: receive remote msg, and handle onTransact()

   status_t BnMyService::onTransact(uint_t code, const Parcel& data,

           Parcel* reply, uint32_t flags) {

       switch(code) {

           case HELLO:

               printf("BnMyService:: got the client hello\n");

               CHECK_INTERFACE(IMyService, data, reply);

               sayHello();

               reply->writeInt32(2016);

               return NO_ERROR;

               break;

           default:

               break;

       }

       return NO_ERROR;

   }

 

   // Server side: sayHello()

   void BnMyService::sayHello() {

       printf("BnMyService::sayHello\n");

   };

}


ServerDemo.cpp:

#include "IMyService.h"

 

int main() {

   sp <IServiceManager> sm = defaultServiceManager();

   sm->addService(String16("service.myservice"), new BnMyService());

   android::ProcessState::self()->startThreadPool();

   IPCThreadState::self()->joinThreadPool();

   return 0;

}

将名为”service.myservice”BnMyService服务添加到ServiceManager,并启动服


ClientDemo.cpp

#include "IMyService.h"

 

int main () {

   sp <IServiceManager> sm = defaultServiceManager();

   sp <IBinder> binder = sm->getService(String16("service.myservice"));

   sp <IMyService> cs = interface_cast <IMyService> (binder);

 

   cs->sayHello(); // Remote call

 

   return 0;

}

 


编译好后,adb push到手机目录中/system/bin

之后开2个终端:

root@:/ # /system/bin/ServerDemo

BnMyService:: got the client hello

BnMyService::sayHello


root@:/ # /system/bin/ClientDemo

BpMyService::SayHello

get num from BnService: 2016


https://www.processon.com/view/link/583feda2e4b08e313590a25d 



 


 




0 0
原创粉丝点击