Chinaunix首页 | 论坛 | 博客
  • 博客访问: 360445
  • 博文数量: 79
  • 博客积分: 1270
  • 博客等级: 中尉
  • 技术积分: 1370
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-12 08:48
个人简介

freedom~~~~~~~~~~

文章分类

全部博文(79)

文章存档

2014年(10)

2013年(2)

2012年(13)

2011年(54)

分类: C/C++

2012-02-14 10:35:55

service
  1. #include
  2. #include
  3. #include
  4. #include
  5. #include
  6. using namespace android;
  7. #ifdef LOG_TAG
  8. #undef LOG_TAG
  9. #endif
  10. #define LOG_TAG "testService"
  11. class MyService : public BBinder
  12. {
  13. public:
  14. MyService()
  15. {
  16. mydescriptor = String16("media.hello");
  17. n=0;
  18. }
  19. virtual ~MyService() {}
  20. //This function is used when call Parcel::checkInterface(IBinder*)
  21. virtual const String16& getInterfaceDescriptor() const
  22. {
  23. LOGE("this is enter ==========getInterfaceDescriptor");
  24. return mydescriptor;
  25. }
  26. protected:
  27. void show()
  28. {
  29. LOGE("this is for test show!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  30. LOGE("this is for test show!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  31. LOGE("this is for test show!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  32. LOGE("this is for test show!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  33. LOGE("this is for test show!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  34. }
  35. virtual status_t onTransact( uint32_t code,
  36. const Parcel& data,
  37. Parcel* reply,
  38. uint32_t flags = 0)
  39. {
  40. LOGD("enter MyService onTransact and the code is %d",code);
  41. /*
  42. if (data.checkInterface(this))
  43. LOGD("checkInterface OK");
  44. else
  45. {
  46. LOGW("checkInterface failed");
  47. return BBinder::onTransact(code, data, reply, flags);
  48. }
  49. */
  50. switch (code)
  51. {
  52. case 1:
  53. LOGD("MyService interface 1");
  54. break;
  55. case 2:
  56. LOGD("MyService interface 2");
  57. cb = data.readStrongBinder();
  58. break;
  59. case 3:
  60. {
  61. LOGD("MyService interface 3, exit");
  62. //No unregister service routine?
  63. //It should return to client first and then call exit in another place.
  64. exit(0);
  65. break;
  66. }
  67. case 4:
  68. {//call cb
  69. LOGD("MyService interface 4 before if================");
  70. cb = data.readStrongBinder();
  71. if (cb != NULL)
  72. {
  73. LOGD("MyService interface 4");
  74. Parcel in, out;
  75. in.writeInterfaceToken(String16("ghq.callback"));
  76. in.writeInt32(n++);
  77. in.writeString16(String16("This is a string."));
  78. cb->transact(1, in, &out, 0);
  79. show();
  80. }
  81. break;
  82. }
  83. default:
  84. return BBinder::onTransact(code, data, reply, flags);
  85. }
  86. return 0;
  87. }
  88. private:
  89. String16 mydescriptor;
  90. sp cb;
  91. int n;
  92. };
  93. int main()
  94. {
  95. sp sm = defaultServiceManager();
  96. status_t ret;
  97. //register MyService to ServiceManager
  98. MyService* srv = new MyService();
  99. ret = sm->addService(String16("media.hello"), srv);
  100. LOGD("addservice media.hello return %d", ret);
  101. //call binder thread pool to start
  102. ProcessState::self()->startThreadPool();
  103. IPCThreadState::self()->joinThreadPool(true);
  104. return 0;
  105. }

client
  1. #include
  2. #include
  3. #include
  4. #include
  5. #include
  6. #include
  7. using namespace android;
  8. #ifdef LOG_TAG
  9. #undef LOG_TAG
  10. #endif
  11. #define LOG_TAG "testCallback"
  12. class MySetupCallback : public BBinder
  13. {
  14. public:
  15. MySetupCallback()
  16. {
  17. mydescriptor = String16("android.os.ISetupCallback");
  18. }
  19. virtual ~MySetupCallback() {}
  20. virtual const String16& getInterfaceDescriptor() const
  21. {
  22. return mydescriptor;
  23. }
  24. protected:
  25. virtual status_t onTransact( uint32_t code,
  26. const Parcel& data,
  27. Parcel* reply,
  28. uint32_t flags = 0)
  29. {
  30. LOGD("enter MySetupCallback onTransact, code=%u", code);
  31. if (data.checkInterface(this))
  32. LOGD("checkInterface OK");
  33. else
  34. {
  35. LOGW("checkInterface failed");
  36. return -1;
  37. }
  38. switch (code)
  39. {
  40. default:
  41. break;
  42. }
  43. return 0;
  44. }
  45. private:
  46. String16 mydescriptor;
  47. };
  48. int main()
  49. {
  50. sp sm = defaultServiceManager();
  51. sp b = sm->getService(String16("media.hello")); //Context.MCU_SERVICE
  52. if (b == NULL)
  53. {
  54. LOGW("Can't find binder service \"media.hello\"");
  55. return -1;
  56. }
  57. Parcel in1,out1;
  58. MySetupCallback *cb = new MySetupCallback();
  59. in1.writeStrongBinder(sp(cb));
  60. int ret = b->transact(4, in1, &out1, 0); //TRANSACTION_registerSetup = 4
  61. LOGD("transact(4) return %d", ret);
  62. ProcessState::self()->startThreadPool();
  63. IPCThreadState::self()->joinThreadPool();
  64. return 0;
  65. }

Android.mk
  1. # Copyright 2006 The Android Open Source Project

  2. LOCAL_PATH:= $(call my-dir)
  3. include $(CLEAR_VARS)

  4. LOCAL_MODULE_TAGS := eng

  5. LOCAL_SRC_FILES:= \
  6.        testCallback.cpp

  7. LOCAL_C_INCLUDES += \
  8.         $(LOCAL_PATH) \
  9.         $(LOCAL_PATH)/../include \
  10.         $(LOCAL_PATH)/../include/callback \


  11. LOCAL_SHARED_LIBRARIES := \
  12.     libcutils \
  13.     libbinder \
  14.     libutils \
  15.     libhardware

  16. LOCAL_CFLAGS := -DRIL_SHLIB

  17. LOCAL_MODULE:= testCallback

  18. include $(BUILD_EXECUTABLE)

  19. include $(CLEAR_VARS)

  20. LOCAL_MODULE_TAGS := eng

  21. LOCAL_SRC_FILES:= \
  22.            testService.cpp

  23. LOCAL_C_INCLUDES += \
  24.         $(LOCAL_PATH) \
  25.         $(LOCAL_PATH)/../include \
  26.         $(LOCAL_PATH)/../include/callback \


  27. LOCAL_SHARED_LIBRARIES := \
  28.     libcutils \
  29.     libbinder \
  30.     libutils \
  31.     libhardware

  32. LOCAL_CFLAGS := -DRIL_SHLIB

  33. LOCAL_MODULE:= testService

  34. include $(BUILD_EXECUTABLE)
阅读(10113) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~