Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2476371
  • 博文数量: 293
  • 博客积分: 2660
  • 博客等级: 少校
  • 技术积分: 3632
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-03 17:50
文章分类

全部博文(293)

文章存档

2015年(13)

2014年(58)

2013年(73)

2012年(25)

2011年(30)

2010年(86)

2009年(8)

分类: Android平台

2014-03-30 13:21:46

参考:http://blog.csdn.net/ljsbuct/article/details/7108578
          http://blog.csdn.net/blackboyofsnp/article/details/7243070
在android源代码下新建一个nativeservice的目录,在下面分别创建'add','application','jni','service'几个目录。
1.service下有AddService.h、AddService.cpp、addserver.cpp、Android.mk几个文件
  AddService.h:
   
点击(此处)折叠或打开
  1. #ifndef ANDROID_ADDSERVICE_H
  2. #define ANDROID_ADDSERVICE_H

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

  6. namespace android
  7. {
  8.     class AddService : public BBinder
  9.     {
  10.     private:
  11.         mutable Mutex m_Lock;
  12.         int32_t m_NextConnId;

  13.     public:
  14.         static int Instance();
  15.         AddService();
  16.         virtual ~AddService();
  17.         virtual status_t onTransact(uint32_t, const Parcel&, Parcel*, uint32_t);
  18.     };
  19. }

  20. #endif
AddService.cpp

点击(此处)折叠或打开

  1. #include <binder/IServiceManager.h>
  2. #include <binder/IPCThreadState.h>
  3. #include "AddService.h"

  4. namespace android
  5. {
  6.     static struct sigaction oldact;
  7.     static pthread_key_t sigbuskey;
  8.     
  9.     int AddService::Instance()
  10.     {
  11.         LOGE("ZPService Instantiate\n");
  12.         int ret = defaultServiceManager()->addService(
  13.                 String16("zp.svc"), new AddService());
  14.         LOGE("AddService ret = %d\n", ret);
  15.         return ret;
  16.     }

  17.     AddService::AddService()
  18.     {
  19.         LOGV("AddService create\n");
  20.         //m_NextConnId = 1;
  21.         pthread_key_create(&sigbuskey,NULL);
  22.     }

  23.     AddService::~AddService()
  24.     {
  25.         pthread_key_delete(sigbuskey);
  26.         LOGV("AddService destory\n");
  27.     }

  28.     status_t AddService::onTransact(uint32_t code,
  29.                                  const Parcel& data,
  30.                                  Parcel* reply,
  31.                                  uint32_t flags)
  32.     {
  33.         switch(code)
  34.         {
  35.         case 0:
  36.             {
  37.                 pid_t pid = data.readInt32();
  38.                 int num = data.readInt32();
  39.                 num += 1000;
  40.                 reply->writeInt32(num);
  41.                 return NO_ERROR;
  42.             } break;
  43.         default:
  44.             return BBinder::onTransact(code, data, reply, flags);
  45.         }
  46.     }
  47. }
addserver.cpp

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <grp.h>
  5. #include <binder/IPCThreadState.h>
  6. #include <binder/ProcessState.h>
  7. #include <binder/IServiceManager.h>
  8. #include <utils/Log.h>
  9. #include "AddService.h"

  10. using namespace android;


  11. int main(int arg, char** argv)
  12. {
  13.     printf("server - ain() begin\n");
  14.     sp<ProcessState> proc(ProcessState::self());
  15.     sp<IServiceManager> sm = defaultServiceManager();
  16.     //LOGI("ServiceManager: %p\n", sm.get());
  17.     printf("server - erviceManager: %p\n", sm.get());
  18.     int ret = AddService::Instance();
  19.     printf("server - ZPService::Instance return %d\n", ret);
  20.     ProcessState::self()->startThreadPool();

  21.         IPCThreadState::self()->joinThreadPool();
  22.         return 0;
  23. }
Android.mk

点击(此处)折叠或打开

  1. LOCAL_PATH:=$(call my-dir)
  2. include $(CLEAR_VARS)
  3. LOCAL_SRC_FILES:=AddService.cpp addserver.cpp
  4. LOCAL_SHARED_LIBRARIES:=libutils libbinder
  5. LOCAL_MODULE_TAGS:=optional
  6. LOCAL_MODULE:=addservice
  7. LOCAL_PRELINK_MODULE:=false
  8. #include $(BUILD_SHARED_LIBRARY)
  9. include $(BUILD_EXECUTABLE)
  在service目录下执行mm,可以生成可执行文件addservice。

2.Add目录下有‘Add.cpp’,‘Add.h’,'Android.mk'几个文件
 Add.h

点击(此处)折叠或打开

  1. #ifndef _Included_Add
  2. #define _Included_Add

  3. namespace android
  4. {
  5.     class Add
  6.     {
  7.     public:
  8.         int setN(int n);
  9.     private:
  10.         static void getAddService();
  11.     };
  12. }
  13. #endif
Add.cpp

点击(此处)折叠或打开

  1. #include <binder/IServiceManager.h>
  2. #include <binder/IPCThreadState.h>
  3. #include <Add.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <jni.h>
  7. namespace android
  8. {
  9.     sp<IBinder> binder;
  10.     
  11.     int Add::setN(int n)
  12.     {
  13.         getAddService();
  14.         Parcel data, reply;
  15.         data.writeInt32(getpid());
  16.         data.writeInt32(n);
  17.         
  18.         LOGE("client - binder->transact()\n");
  19.         binder->transact(0, data, &reply);
  20.         int r = reply.readInt32();
  21.         return r;
  22.     }

  23.     void Add::getAddService()
  24.     {
  25.         sp<IServiceManager> sm = defaultServiceManager();
  26.         binder = sm->getService(String16("zp.svc"));
  27.         LOGE("client - etService: %p\n", sm.get());
  28.         if(binder == 0)
  29.         {
  30.             LOGW("ZPService not published, waiting...");
  31.             return;
  32.         }
  33.     }

  34. };
Android.mk

点击(此处)折叠或打开

  1. LOCAL_PATH:=$(call my-dir)
  2. include $(CLEAR_VARS)
  3. LOCAL_SRC_FILES:=Add.cpp
  4. LOCAL_SHARED_LIBRARIES:=libutils libbinder
  5. LOCAL_MODULE_TAGS:=optional
  6. LOCAL_MODULE:=libAdd
  7. LOCAL_PRELINK_MODULE:=false
  8. include $(BUILD_SHARED_LIBRARY)
  在service目录下执行mm,可以生成库文件libAdd.so
3.jni目录下有‘com_hello_Service_MySer.h’,‘com_hello_Service_MySer.cpp’,'Android.mk'几个文件
com_hello_Service_MySer.h

点击(此处)折叠或打开

  1. /* DO NOT EDIT THIS FILE - it is machine generated */
  2. #include <jni.h>
  3. /* Header for class com_hello_Service_MySer */
  4. #ifndef _Included_com_hello_Service_MySer
  5. #define _Included_com_hello_Service_MySer
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. /*
  10.   * Class: com_hello_Service_MySer
  11.   * Method: intFromJNI
  12.   * Signature: ()I
  13.   */
  14. JNIEXPORT jint JNICALL Java_com_hello_Service_MySer_intFromJNI
  15.   (JNIEnv *, jobject);
  16. #ifdef __cplusplus
  17. }
  18. #endif
  19. #endif
com_hello_Service_MySer.cpp

点击(此处)折叠或打开

  1. #include <jni.h>
  2. #include <JNIHelp.h>
  3. #include "../add/Add.h"
  4. #include "com_hello_Service_MySer.h"

  5. using namespace android;
  6. JNIEXPORT jint JNICALL Java_com_hello_Service_MySer_intFromJNI(JNIEnv * env, jobject)
  7. {
  8.     Add* p = new Add();
  9.     int r = p->setN(5);
  10.     return r;
  11. }
Android.mk

点击(此处)折叠或打开

  1. LOCAL_PATH:=$(call my-dir)
  2. include $(CLEAR_VARS)
  3. LOCAL_SRC_FILES:=com_hello_Service_MySer.cpp
  4. LOCAL_SHARED_LIBRARIES:=libutils libAdd
  5. LOCAL_MODULE_TAGS:=optional
  6. LOCAL_C_INCLUDES:= $(JNI_H_INCLUDE)
  7. LOCAL_MODULE:=libJniAdd
  8. LOCAL_PRELINK_MODULE:=false
  9. include $(BUILD_SHARED_LIBRARY)
  在service目录下执行mm,可以生成库文件libJniAdd.so
4
.application目录下是一个app工程
MySer.java:

点击(此处)折叠或打开

  1. package com.hello.Service;


  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import android.widget.TextView;

  6. public class MySer extends Activity {

  7.     private TextView tv;
  8.     @Override
  9.     protected void onCreate(Bundle savedInstanceState) {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.activity_my_ser);
  12.         int i = intFromJNI();
  13.         tv = (TextView)findViewById(R.id.tv);
  14.         tv.setText( String.valueOf(i) );
  15.     }
  16.     @Override
  17.     public boolean onCreateOptionsMenu(Menu menu) {
  18.         // Inflate the menu; this adds items to the action bar if it is present.
  19.         getMenuInflater().inflate(R.menu.my, menu);
  20.         return true;
  21.     }
  22.      public native int intFromJNI();
  23.      static {
  24.      System.loadLibrary("JniAdd");
  25.      }

  26. }
可以生成Service.apk。
5.Service.apk 安装到手机
   
addservice push到/system/bin
   libAdd.solibJniAdd.so push到/system/lib
6.修改init.rc,在下面新增:
  

点击(此处)折叠或打开

  1. service addservice /system/bin/addservice
  2. class main
  3. user root
这样就新增了一个服务,通过ps ,service list 或getprop都可以查看该服务
6.运行app









   
阅读(8362) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~