Enable Native service in Android to use binder to communicate with Applications

Source: Internet
Author: User

The upper-layer Android applications call the native service through the Binder Mechanism. The figure below mainly describes the basic class diagrams when we implement the BP and Bn related to the binder.


The basic description is how to use binder to implement the underlying service. The basic implementation is the template above. Below I will introduce a demo implemented by myself. I will explain in detail how to write code, I used to read other people's blogs and refer to the examples. However, if there are many problems with this example, I changed it and it can be used normally.

Is basically explained, my demo class inheritance relationship, this demo mainly implements the Java upper layer using binder to call the BN end of the C ++ layer, then the BN end of C ++ returns the request to the upper layer. For such an instance, this instance mainly performs an addition operation.

Native services implemented by C ++


Addservice. h

#ifndef ANDROID_GUILH_ADD_SERVICE_H#define ANDROID_GUILH_ADD_SERVICE_H#include <utils/threads.h>#include <utils/RefBase.h>#include <binder/IInterface.h>#include <binder/Parcel.h>#include <binder/IBinder.h>#include <binder/Binder.h>#include <binder/IBinder.h>#include <binder/IServiceManager.h>#include <utils/Log.h>#include <utils/misc.h>#include <binder/Parcel.h>#include <utils/StringArray.h>#include <utils/threads.h>#include <cutils/properties.h>#include "jni.h"#include "JNIHelp.h"#include <stdio.h>#include <signal.h>#include <sys/stat.h>#include <sys/types.h>#include <signal.h>#include <dirent.h>#include <assert.h>namespace android{class AddService : public BBinder{mutable Mutex mLock;int32_t mNextConnId;public:static int instantiate();AddService();virtual ~AddService();virtual status_t onTransact(uint32_t, const Parcel&, Parcel*, uint32_t);};}#endif

Addservice. cpp

#include <AddService.h>#include <binder/IServiceManager.h>#include <utils/Debug.h>#include <utils/Log.h>#include <binder/IPCThreadState.h>#include <binder/Parcel.h>//#include <binder/IBinder.h>#include <utils/String8.h>#include <binder/IBinder.h>#include <binder/IServiceManager.h>#include <utils/Log.h>#include <utils/misc.h>#include <binder/Parcel.h>#include <utils/StringArray.h>#include <utils/threads.h>#include <cutils/properties.h>#include "jni.h"#include "JNIHelp.h"//#include "android_util_Binder.h"#include <stdio.h>#include <signal.h>#include <sys/stat.h>#include <sys/types.h>#include <signal.h>#include <dirent.h>#include <assert.h>namespace android {static struct sigaction oldact;static pthread_key_t sigbuskey;sp<IBinder> binder;int AddService::instantiate() {LOGE("AddService instantiate");int r = defaultServiceManager()->addService(String16("flyfot.add"), new AddService());LOGE("AddService r = %d/n", r);return r;}AddService::AddService(){LOGV("AddService created");mNextConnId = 1;pthread_key_create(&sigbuskey, NULL);}AddService::~AddService(){pthread_key_delete(sigbuskey);LOGV("AddService destroyed");}status_t AddService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {LOGE("AddDemo", "AddService.cpp:onTransact:" + code);switch (code) {case 0: {LOGI("AddDemo", "addservice::onTransact");//pid_t pid = data.readInt32();//int a = data.readInt32();//int b = data.readInt32();//int num = a + b;//sp < IServiceManager > sm = defaultServiceManager();LOGE("AddService","start get java service");//binder = sm->getService(//String16("zx.java"));//String16 info = data.re//data.enbinder = data.readStrongBinder();LOGE("AddService.cpp","data.readStrongBinbder");if (binder == 0) {LOGE("AddService", "get java local service failed");return NO_ERROR;}int a = data.readInt32();int b = data.readInt32();Parcel _data_sec ;//String16 interface = "cn.zx.callback";_data_sec.writeInterfaceToken(String16("cn.zx.secrity.RemoteSecrityCallback"));_data_sec.writeInt32(a);_data_sec.writeInt32(b);LOGE("AddService", "get java local service sucessful");binder->transact(0,_data_sec, reply);return NO_ERROR;}break;default:return BBinder::onTransact(code, data, reply, flags);}}};

..... To be continued /.....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.