Android native service实例(一)

AOSP版本:android-7-0-0_r6

项目结构:位于android-7-0-0_r6\frameworks\native\services\thinking_test下


1、service:native service的主体,即服务的实现部分

2、server:native service的载体,即启动和注册服务的部分。

3、client:客户端程序,服务调用封装。

4、test:测试client

------------------------------------------------------------------------------------------------------------------------------------------

service


ThinkingService.h
#ifndef THINKING_SERVICE_H
#define THINKING_SERVICE_H

#include <utils/RefBase.h>
#include <binder/IInterface.h>
#include <binder/Parcel.h>

#include <android/log.h>
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR  , "ProjectName", __VA_ARGS__)

namespace android{
	class ThinkingService : public BBinder
	{
		private:
		
		public:
		//单例模式
		static int Instance();
		ThinkingService();
		virtual ~ThinkingService();
		//预定义通信方法,签名写死
		//参数意义是:通信状态,输入参数,输出参数,执行状态
		virtual status_t onTransact(uint32_t, const Parcel&, Parcel*, uint32_t);
	};
}

#endif
ThinkingService.cpp
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
#include "ThinkingService.h"

namespace android
{
	static pthread_key_t sigbuskey;
	int ThinkingService::Instance()
	{
		LOGE("-----------ThinkingService-->Instance \n");
		int ret=defaultServiceManager()->addService( String16("thinking.svc"),new ThinkingService());
		LOGE("-----------ThinkingService-->Instance %d \n",ret);
		return ret;
	}
	
	ThinkingService::ThinkingService()
	{
		LOGE("-----------ThinkingService");
		pthread_key_create(&sigbuskey,NULL);
	}
	
	ThinkingService::~ThinkingService()
	{
		LOGE("-----------~ThinkingService");
		pthread_key_delete(sigbuskey);
	}
	
	status_t ThinkingService::onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
	{
		switch(code)
		{
			case 0:
			{
				int a=data.readInt32();
				int b=data.readInt32();
				reply->writeInt32(a*b);
				return NO_ERROR;
			}
			default:
				return BBinder::onTransact(code, data, reply, flags);
		}
	}
}
Android.mk
LOCAL_PATH:=$(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:=ThinkingService.cpp
LOCAL_SHARED_LIBRARIES:=libutils libbinder
LOCAL_MODULE_TAGS:=optional
LOCAL_MODULE:=libThinkingService
LOCAL_PRELINK_MODULE:=false
LOCAL_LDLIBS +=  -llog
include $(BUILD_SHARED_LIBRARY)
------------------------------------------------------------------------------------------------------------------------------------------

server



ThinkingServer.cpp
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <grp.h>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <utils/Log.h>
#include "../service/ThinkingService.h"

using namespace android;

int main(int arg, char** argv)
{
	printf("ThinkingService start register \n");
	sp<ProcessState> proc(ProcessState::self());
    sp<IServiceManager> sm = defaultServiceManager();
	int ret=ThinkingService::Instance();
	ProcessState::self()->startThreadPool();
	IPCThreadState::self()->joinThreadPool();
	return 0;
}

Android.mk
LOCAL_PATH:=$(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:=ThinkingServer.cpp
LOCAL_SHARED_LIBRARIES:=libutils libbinder libThinkingService
LOCAL_MODULE_TAGS:=optional
LOCAL_MODULE:=ThinkingServer
include $(BUILD_EXECUTABLE)

------------------------------------------------------------------------------------------------------------------------------------------

client



ThinkingClient.h
#ifndef THINKING_CLIENT_H
#define THINKING_CLIENT_H

namespace android
{
	class ThinkingClient
	{
		public:
		int setData(int a,int b);
		private:
		static void getThinkingService();
	};
}

#endif
ThinkingClient.cpp

#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
#include "ThinkingClient.h"

namespace android
{
	sp<IBinder> binder;
	
	int ThinkingClient::setData(int a,int b)
	{
		getThinkingService();
		Parcel data,reply;
		data.writeInt32(a);
		data.writeInt32(b);
		binder->transact(0, data, &reply);
		int result=reply.readInt32();
		return result;
	}
	
	void ThinkingClient::getThinkingService()
	{
		sp<IServiceManager> sm = defaultServiceManager();
        binder = sm->getService(String16("thinking.svc"));
		if(binder == 0)
			return;
	}
}

Android.mk
LOCAL_PATH:=$(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:=ThinkingClient.cpp
LOCAL_SHARED_LIBRARIES:=libutils libbinder
LOCAL_MODULE_TAGS:=optional
LOCAL_MODULE:=libThinkingClient
LOCAL_PRELINK_MODULE:=false
include $(BUILD_SHARED_LIBRARY)


------------------------------------------------------------------------------------------------------------------------------------------

test




test.cpp

#include <stdio.h>
#include "../client/ThinkingClient.h"

using namespace android;

int main(int argc, char** argv)
{
	ThinkingClient client;
	int result=client.setData(1,2);
	printf("result is %d \n",result);
	return 0;
}

Android.mk
LOCAL_PATH:=$(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:=test.cpp
LOCAL_SHARED_LIBRARIES:=libThinkingClient
LOCAL_MODULE_TAGS:=optional
LOCAL_MODULE:=ThinkingTest
include $(BUILD_EXECUTABLE)



------------------------------------------------------------------------------------------------------------------------------------------

整个项目的Android.mk文件(即android-7-0-0_r6\frameworks\native\services\thinking_test\Android.mk)

include $(call all-subdir-makefiles)
------------------------------------------------------------------------------------------------------------------------------------------

实际验证

1、把这4个部分编译出来

[yong@localhost android-7-0-0_r6]$ cd frameworks/native/services/thinking_test
[yong@localhost thinking_test]$ mm

[yong@localhost android-7-0-0_r6]$ ls out/target/product/angler/system/lib
可以看到
[yong@localhost android-7-0-0_r6]$ ls out/target/product/angler/system/bin
可以看到


2、用adb命令将编译结果push到测试手机里面去

Z:\AndroidSourceCode\android-7-0-0_r6>adb push .\out\target\product\angler\system\lib\libThinkingClient.so /system/lib/
[100%] /system/lib/libThinkingClient.so


Z:\AndroidSourceCode\android-7-0-0_r6>adb push .\out\target\product\angler\system\lib\libThinkingService.so /system/lib/
[100%] /system/lib/libThinkingService.so

Z:\AndroidSourceCode\android-7-0-0_r6>adb push .\out\target\product\angler\system\bin\ThinkingServer /system/bin/
[100%] /system/bin/ThinkingServer


Z:\AndroidSourceCode\android-7-0-0_r6>adb push .\out\target\product\angler\system\bin\ThinkingTest /system/bin/
[100%] /system/bin/ThinkingTest

3、测试
Z:\AndroidSourceCode\android-7-0-0_r6>adb shell
angler:/system/bin # ./ThinkingServer &
[1] 16230
angler:/system/bin # ThinkingService start register
angler:/system/bin # ./ThinkingTest
result is 2
angler:/system/bin #

编译优化

如果嫌mm的方式麻烦
可在android-7-0-0_r6\build\target\product\base.mk添加如下代码
PRODUCT_PACKAGES += \
......
	libThinkingService \
	libThinkingClient \
	ThinkingServer \
	ThinkingTest \
......
即可将这些模块加入AOSP编译系统,这样直接make,就可得到包含这些模块的输出结果(system.img)











  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值