├── .classpath ├── .gitignore ├── .project ├── Android.mk ├── AndroidManifest.xml ├── NOTICE ├── README ├── buildit.sh ├── default.properties ├── deploy.sh ├── helloworld ├── Android.mk └── main_helloworldservice.cpp ├── helloworldclient ├── Android.mk └── main_helloworldclient.cpp ├── libhelloworldservice ├── Android.mk ├── include │ ├── android-IInterface.h │ ├── helloworld.h │ └── helloworldservice.h └── src │ ├── helloworldclient.cpp │ └── helloworldservice.cpp ├── res ├── drawable │ └── icon.png ├── layout │ └── hello.xml └── values │ └── strings.xml └── src └── org └── credil └── helloworldservice ├── HelloWorld.java └── HelloWorldServiceInterface.aidl /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | HelloWorldService 4 | 5 | 6 | 7 | 8 | 9 | com.android.ide.eclipse.adt.ResourceManagerBuilder 10 | 11 | 12 | 13 | 14 | com.android.ide.eclipse.adt.PreCompilerBuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.jdt.core.javabuilder 20 | 21 | 22 | 23 | 24 | com.android.ide.eclipse.adt.ApkBuilder 25 | 26 | 27 | 28 | 29 | 30 | com.android.ide.eclipse.adt.AndroidNature 31 | org.eclipse.jdt.core.javanature 32 | 33 | 34 | -------------------------------------------------------------------------------- /Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH:= $(call my-dir) 2 | include $(CLEAR_VARS) 3 | 4 | LOCAL_MODULE_TAGS := user 5 | 6 | LOCAL_SRC_FILES := $(call all-java-files-under, src) \ 7 | src/org/credil/helloworldservice/HelloWorldServiceInterface.aidl 8 | 9 | LOCAL_PACKAGE_NAME := HelloWorldService 10 | 11 | LOCAL_REQUIRED_MODULES := libhelloworldservice 12 | LOCAL_JNI_SHARED_LIBRARIES := libhelloworldservice 13 | 14 | include $(BUILD_PACKAGE) 15 | 16 | # Use the following include to make our test apk. 17 | include $(call all-makefiles-under,$(LOCAL_PATH)) 18 | -------------------------------------------------------------------------------- /AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | 2 | Copyright (c) 2009, Michael Richardson 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | 7 | Unless required by applicable law or agreed to in writing, software 8 | distributed under the License is distributed on an "AS IS" BASIS, 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 | See the License for the specific language governing permissions and 11 | limitations under the License. 12 | 13 | 14 | Apache License 15 | Version 2.0, January 2004 16 | http://www.apache.org/licenses/ 17 | 18 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 19 | 20 | 1. Definitions. 21 | 22 | "License" shall mean the terms and conditions for use, reproduction, 23 | and distribution as defined by Sections 1 through 9 of this document. 24 | 25 | "Licensor" shall mean the copyright owner or entity authorized by 26 | the copyright owner that is granting the License. 27 | 28 | "Legal Entity" shall mean the union of the acting entity and all 29 | other entities that control, are controlled by, or are under common 30 | control with that entity. For the purposes of this definition, 31 | "control" means (i) the power, direct or indirect, to cause the 32 | direction or management of such entity, whether by contract or 33 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 34 | outstanding shares, or (iii) beneficial ownership of such entity. 35 | 36 | "You" (or "Your") shall mean an individual or Legal Entity 37 | exercising permissions granted by this License. 38 | 39 | "Source" form shall mean the preferred form for making modifications, 40 | including but not limited to software source code, documentation 41 | source, and configuration files. 42 | 43 | "Object" form shall mean any form resulting from mechanical 44 | transformation or translation of a Source form, including but 45 | not limited to compiled object code, generated documentation, 46 | and conversions to other media types. 47 | 48 | "Work" shall mean the work of authorship, whether in Source or 49 | Object form, made available under the License, as indicated by a 50 | copyright notice that is included in or attached to the work 51 | (an example is provided in the Appendix below). 52 | 53 | "Derivative Works" shall mean any work, whether in Source or Object 54 | form, that is based on (or derived from) the Work and for which the 55 | editorial revisions, annotations, elaborations, or other modifications 56 | represent, as a whole, an original work of authorship. For the purposes 57 | of this License, Derivative Works shall not include works that remain 58 | separable from, or merely link (or bind by name) to the interfaces of, 59 | the Work and Derivative Works thereof. 60 | 61 | "Contribution" shall mean any work of authorship, including 62 | the original version of the Work and any modifications or additions 63 | to that Work or Derivative Works thereof, that is intentionally 64 | submitted to Licensor for inclusion in the Work by the copyright owner 65 | or by an individual or Legal Entity authorized to submit on behalf of 66 | the copyright owner. For the purposes of this definition, "submitted" 67 | means any form of electronic, verbal, or written communication sent 68 | to the Licensor or its representatives, including but not limited to 69 | communication on electronic mailing lists, source code control systems, 70 | and issue tracking systems that are managed by, or on behalf of, the 71 | Licensor for the purpose of discussing and improving the Work, but 72 | excluding communication that is conspicuously marked or otherwise 73 | designated in writing by the copyright owner as "Not a Contribution." 74 | 75 | "Contributor" shall mean Licensor and any individual or Legal Entity 76 | on behalf of whom a Contribution has been received by Licensor and 77 | subsequently incorporated within the Work. 78 | 79 | 2. Grant of Copyright License. Subject to the terms and conditions of 80 | this License, each Contributor hereby grants to You a perpetual, 81 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 82 | copyright license to reproduce, prepare Derivative Works of, 83 | publicly display, publicly perform, sublicense, and distribute the 84 | Work and such Derivative Works in Source or Object form. 85 | 86 | 3. Grant of Patent License. Subject to the terms and conditions of 87 | this License, each Contributor hereby grants to You a perpetual, 88 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 89 | (except as stated in this section) patent license to make, have made, 90 | use, offer to sell, sell, import, and otherwise transfer the Work, 91 | where such license applies only to those patent claims licensable 92 | by such Contributor that are necessarily infringed by their 93 | Contribution(s) alone or by combination of their Contribution(s) 94 | with the Work to which such Contribution(s) was submitted. If You 95 | institute patent litigation against any entity (including a 96 | cross-claim or counterclaim in a lawsuit) alleging that the Work 97 | or a Contribution incorporated within the Work constitutes direct 98 | or contributory patent infringement, then any patent licenses 99 | granted to You under this License for that Work shall terminate 100 | as of the date such litigation is filed. 101 | 102 | 4. Redistribution. You may reproduce and distribute copies of the 103 | Work or Derivative Works thereof in any medium, with or without 104 | modifications, and in Source or Object form, provided that You 105 | meet the following conditions: 106 | 107 | (a) You must give any other recipients of the Work or 108 | Derivative Works a copy of this License; and 109 | 110 | (b) You must cause any modified files to carry prominent notices 111 | stating that You changed the files; and 112 | 113 | (c) You must retain, in the Source form of any Derivative Works 114 | that You distribute, all copyright, patent, trademark, and 115 | attribution notices from the Source form of the Work, 116 | excluding those notices that do not pertain to any part of 117 | the Derivative Works; and 118 | 119 | (d) If the Work includes a "NOTICE" text file as part of its 120 | distribution, then any Derivative Works that You distribute must 121 | include a readable copy of the attribution notices contained 122 | within such NOTICE file, excluding those notices that do not 123 | pertain to any part of the Derivative Works, in at least one 124 | of the following places: within a NOTICE text file distributed 125 | as part of the Derivative Works; within the Source form or 126 | documentation, if provided along with the Derivative Works; or, 127 | within a display generated by the Derivative Works, if and 128 | wherever such third-party notices normally appear. The contents 129 | of the NOTICE file are for informational purposes only and 130 | do not modify the License. You may add Your own attribution 131 | notices within Derivative Works that You distribute, alongside 132 | or as an addendum to the NOTICE text from the Work, provided 133 | that such additional attribution notices cannot be construed 134 | as modifying the License. 135 | 136 | You may add Your own copyright statement to Your modifications and 137 | may provide additional or different license terms and conditions 138 | for use, reproduction, or distribution of Your modifications, or 139 | for any such Derivative Works as a whole, provided Your use, 140 | reproduction, and distribution of the Work otherwise complies with 141 | the conditions stated in this License. 142 | 143 | 5. Submission of Contributions. Unless You explicitly state otherwise, 144 | any Contribution intentionally submitted for inclusion in the Work 145 | by You to the Licensor shall be under the terms and conditions of 146 | this License, without any additional terms or conditions. 147 | Notwithstanding the above, nothing herein shall supersede or modify 148 | the terms of any separate license agreement you may have executed 149 | with Licensor regarding such Contributions. 150 | 151 | 6. Trademarks. This License does not grant permission to use the trade 152 | names, trademarks, service marks, or product names of the Licensor, 153 | except as required for reasonable and customary use in describing the 154 | origin of the Work and reproducing the content of the NOTICE file. 155 | 156 | 7. Disclaimer of Warranty. Unless required by applicable law or 157 | agreed to in writing, Licensor provides the Work (and each 158 | Contributor provides its Contributions) on an "AS IS" BASIS, 159 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 160 | implied, including, without limitation, any warranties or conditions 161 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 162 | PARTICULAR PURPOSE. You are solely responsible for determining the 163 | appropriateness of using or redistributing the Work and assume any 164 | risks associated with Your exercise of permissions under this License. 165 | 166 | 8. Limitation of Liability. In no event and under no legal theory, 167 | whether in tort (including negligence), contract, or otherwise, 168 | unless required by applicable law (such as deliberate and grossly 169 | negligent acts) or agreed to in writing, shall any Contributor be 170 | liable to You for damages, including any direct, indirect, special, 171 | incidental, or consequential damages of any character arising as a 172 | result of this License or out of the use or inability to use the 173 | Work (including but not limited to damages for loss of goodwill, 174 | work stoppage, computer failure or malfunction, or any and all 175 | other commercial damages or losses), even if such Contributor 176 | has been advised of the possibility of such damages. 177 | 178 | 9. Accepting Warranty or Additional Liability. While redistributing 179 | the Work or Derivative Works thereof, You may choose to offer, 180 | and charge a fee for, acceptance of support, warranty, indemnity, 181 | or other liability obligations and/or rights consistent with this 182 | License. However, in accepting such obligations, You may act only 183 | on Your own behalf and on Your sole responsibility, not on behalf 184 | of any other Contributor, and only if You agree to indemnify, 185 | defend, and hold each Contributor harmless for any liability 186 | incurred by, or claims asserted against, such Contributor by reason 187 | of your accepting any such warranty or additional liability. 188 | 189 | END OF TERMS AND CONDITIONS 190 | 191 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | I wrote a dummy "HelloWorld" service and client. 2 | It uses Binder for IPC. For some this is likely "duh", but it did take me 3 | several days of puzzling to work things out. I think some of the problem is 4 | that there is some dead code in IMediaPlayerClient.cpp, which I was using as 5 | my guide. 6 | 7 | I'm convinced that it should be possible to see the services from Java 8 | Binder/ServiceManager code, and avoid a layer of JNI (and thus an NDK 9 | invocation) between a Java application and a C++ service, but I didn't manage 10 | that. The bird droppings in the src/org/credil/...*.java are my attempts, 11 | ignore them, or better yet, fix them. 12 | 13 | Push the three pieces generated: libhelloworldservice.so (to /system/lib), 14 | and helloworldservice, and helloworldclient to /data. 15 | (See deploy.sh, and buildit.sh) 16 | 17 | adb shell 18 | cd /data 19 | ./helloworldservice & 20 | ./helloworldclient 21 | 22 | I hope that this code will help others from scratching their head a lot. 23 | 24 | https://review.source.android.com/12454 25 | is one suggestion I have to make other writers of code like this easier. 26 | 27 | Probably I'm posting to the wrong mailing list, sorry. I can not keep up 28 | with these mailing lists. 29 | 30 | My code is at: 31 | http://github.com/mcr/Android-HelloWorldService 32 | 33 | -------------------------------------------------------------------------------- /buildit.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # use this as the "make" command under Emacs... 4 | 5 | here=`pwd` 6 | cd ../../.. 7 | source build/envsetup.sh 8 | 9 | cd - 10 | mm 11 | -------------------------------------------------------------------------------- /default.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system use, 7 | # "build.properties", and override values to adapt the script to your 8 | # project structure. 9 | 10 | # Project target. 11 | target=android-3 12 | -------------------------------------------------------------------------------- /deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | set -e 4 | cd ../../.. 5 | adb push out/target/product/generic/system/bin/helloworldservice /data 6 | adb push out/target/product/generic/system/bin/helloworldclient /data 7 | adb push out/target/product/generic/system/lib/libhelloworldservice.so /system/lib 8 | 9 | -------------------------------------------------------------------------------- /helloworld/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | 3 | helloworldservice_module_tags := eng user 4 | 5 | include $(CLEAR_VARS) 6 | 7 | LOCAL_SRC_FILES := main_helloworldservice.cpp 8 | 9 | LOCAL_C_INCLUDES := $(LOCAL_PATH)/../libhelloworldservice/include 10 | 11 | LOCAL_CFLAGS += -DPLATFORM_ANDROID 12 | 13 | LOCAL_MODULE := helloworldservice 14 | 15 | # for now, until I do a full rebuild. 16 | LOCAL_PRELINK_MODULE := false 17 | 18 | # LOCAL_LDFLAGS += -llog 19 | 20 | LOCAL_SHARED_LIBRARIES += liblog 21 | LOCAL_SHARED_LIBRARIES += libutils libui 22 | LOCAL_SHARED_LIBRARIES += libhelloworldservice 23 | 24 | LOCAL_CFLAGS += -Idalvik/libnativehelper/include/nativehelper 25 | 26 | include $(BUILD_EXECUTABLE) 27 | 28 | -------------------------------------------------------------------------------- /helloworld/main_helloworldservice.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HelloWorld Service driver. 3 | * Copyright (C) 2009 Michael Richardson 4 | * 5 | * Released under the terms of the file ../NOTICE 6 | */ 7 | #define LOG_TAG "HelloWorld/Service" 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include "helloworldservice.h" 19 | 20 | int main(int argc, char *argv[]) 21 | { 22 | HelloWorldService::instantiate(); 23 | android::ProcessState::self()->startThreadPool(); 24 | LOGI("Hello Service is now ready"); 25 | 26 | android::IPCThreadState::self()->joinThreadPool(); 27 | return(0); 28 | } 29 | -------------------------------------------------------------------------------- /helloworldclient/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | 3 | helloworldservice_module_tags := eng user 4 | 5 | include $(CLEAR_VARS) 6 | 7 | LOCAL_SRC_FILES := main_helloworldclient.cpp 8 | 9 | LOCAL_C_INCLUDES := $(LOCAL_PATH)/../libhelloworldservice/include 10 | 11 | LOCAL_CFLAGS += -DPLATFORM_ANDROID 12 | 13 | LOCAL_MODULE := helloworldclient 14 | 15 | # for now, until I do a full rebuild. 16 | LOCAL_PRELINK_MODULE := false 17 | 18 | # LOCAL_LDFLAGS += -llog 19 | 20 | LOCAL_SHARED_LIBRARIES += liblog 21 | LOCAL_SHARED_LIBRARIES += libutils libui 22 | LOCAL_SHARED_LIBRARIES += libhelloworldservice 23 | 24 | LOCAL_CFLAGS += -Idalvik/libnativehelper/include/nativehelper 25 | 26 | include $(BUILD_EXECUTABLE) 27 | 28 | -------------------------------------------------------------------------------- /helloworldclient/main_helloworldclient.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HelloWorld Service driver. 3 | * Copyright (C) 2009 Michael Richardson 4 | * 5 | * Released under the terms of the file ../NOTICE 6 | */ 7 | #define LOG_TAG "HelloWorld/Service" 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | #include 16 | #include 17 | 18 | #include "helloworld.h" 19 | 20 | int main(int argc, char *argv[]) 21 | { 22 | LOGI("Hello client is now starting"); 23 | 24 | android::sp sm = android::defaultServiceManager(); 25 | android::sp binder; 26 | android::sp shw; 27 | 28 | do { 29 | binder = sm->getService(android::String16(HELLOWORLD_NAME)); 30 | if (binder != 0) 31 | break; 32 | LOGW("HelloWorld not published, waiting..."); 33 | usleep(500000); // 0.5 s 34 | } while(true); 35 | 36 | //LOGI("sm: %u binder: %u", sm, binder); 37 | 38 | //if (sDeathNotifier == NULL) { 39 | // sDeathNotifier = new DeathNotifier(); 40 | //} 41 | //binder->linkToDeath(sDeathNotifier); 42 | //sMediaPlayerService = interface_cast(binder); 43 | 44 | LOGI("Hello client is now trying"); 45 | 46 | shw = android::interface_cast(binder); 47 | shw->hellothere("fun"); 48 | 49 | LOGI("Hello client is now exiting"); 50 | 51 | return(0); 52 | } 53 | -------------------------------------------------------------------------------- /libhelloworldservice/Android.mk: -------------------------------------------------------------------------------- 1 | LOCAL_PATH := $(call my-dir) 2 | 3 | tw_module_tags := eng user 4 | 5 | include $(CLEAR_VARS) 6 | 7 | LOCAL_SRC_FILES := src/helloworldservice.cpp 8 | LOCAL_SRC_FILES += src/helloworldclient.cpp 9 | 10 | LOCAL_C_INCLUDES := \ 11 | $(LOCAL_PATH)/include 12 | 13 | LOCAL_CFLAGS += -DPLATFORM_ANDROID 14 | 15 | LOCAL_MODULE_TAGS := $(tw_module_tags) 16 | 17 | LOCAL_MODULE := libhelloworldservice 18 | 19 | # for now, until I do a full rebuild. 20 | LOCAL_PRELINK_MODULE := false 21 | 22 | # LOCAL_LDFLAGS += -llog 23 | 24 | LOCAL_SHARED_LIBRARIES += liblog 25 | LOCAL_SHARED_LIBRARIES += libutils libui 26 | 27 | LOCAL_CFLAGS += -Idalvik/libnativehelper/include/nativehelper 28 | 29 | include $(BUILD_SHARED_LIBRARY) 30 | -------------------------------------------------------------------------------- /libhelloworldservice/include/android-IInterface.h: -------------------------------------------------------------------------------- 1 | /* 2 | * These are repeats of macros found in utils/IInterface.h. 3 | * the version there, does not use the proper namespace, so you have 4 | * to write them in "android" namespace. 5 | */ 6 | 7 | #define android_DECLARE_META_INTERFACE(INTERFACE) \ 8 | static const android::String16 descriptor; \ 9 | static android::sp asInterface(const android::sp& obj); \ 10 | static android::String16 getInterfaceDescriptor(); \ 11 | 12 | #define android_IMPLEMENT_META_INTERFACE(INTERFACE, NAME) \ 13 | const android::String16 I##INTERFACE::descriptor(NAME); \ 14 | android::String16 I##INTERFACE::getInterfaceDescriptor() { \ 15 | return I##INTERFACE::descriptor; \ 16 | } \ 17 | android::sp I##INTERFACE::asInterface(const android::sp& obj) \ 18 | { \ 19 | android::sp intr; \ 20 | if (obj != NULL) { \ 21 | intr = static_cast( \ 22 | obj->queryLocalInterface( \ 23 | I##INTERFACE::descriptor).get()); \ 24 | if (intr == NULL) { \ 25 | intr = new Bp##INTERFACE(obj); \ 26 | } \ 27 | } \ 28 | return intr; \ 29 | } \ 30 | 31 | -------------------------------------------------------------------------------- /libhelloworldservice/include/helloworld.h: -------------------------------------------------------------------------------- 1 | #ifndef _HELLOWORLD_H_ 2 | #define _HELLOWORLD_H_ 3 | 4 | #define HELLOWORLD_NAME "org.credil.helloworldservice.HelloWorldServiceInterface" 5 | 6 | #include "android-IInterface.h" 7 | 8 | enum { 9 | HW_HELLOTHERE=1, 10 | }; 11 | 12 | 13 | class IHelloWorldClient: public android::IInterface { 14 | public: 15 | android_DECLARE_META_INTERFACE(HelloWorldClient) // no trailing ; 16 | 17 | virtual void hellothere(const char *str) = 0; 18 | }; 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /libhelloworldservice/include/helloworldservice.h: -------------------------------------------------------------------------------- 1 | #ifndef _HELLOWORLDSERVICE_H_ 2 | #define _HELLOWORLDSERVICE_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | 14 | #include "helloworld.h" 15 | 16 | class IHelloWorldService: public android::IInterface { 17 | public: 18 | 19 | android_DECLARE_META_INTERFACE(HelloWorldService) 20 | 21 | void hellothere(const char *str); 22 | }; 23 | 24 | class BnHelloWorldService : public android::BnInterface 25 | { 26 | // not sure. 27 | // actual dispatch. 28 | }; 29 | 30 | class HelloWorldService : public BnHelloWorldService 31 | { 32 | class Client; 33 | 34 | public: 35 | static void instantiate(); 36 | 37 | // class Client : public BnMediaPlayer { 38 | // 39 | // // IHelloWorld interface 40 | // } 41 | 42 | HelloWorldService(); 43 | virtual ~HelloWorldService(); 44 | 45 | android::status_t onTransact(uint32_t code, 46 | const android::Parcel &data, 47 | android::Parcel *reply, 48 | uint32_t flags); 49 | 50 | mutable android::Mutex mLock; 51 | android::SortedVector< android::wp > mClients; 52 | int32_t mNextConnId; 53 | }; 54 | 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /libhelloworldservice/src/helloworldclient.cpp: -------------------------------------------------------------------------------- 1 | #define LOG_TAG "HelloWorldService-JNI" 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include "helloworld.h" 18 | #include "helloworldservice.h" 19 | #include "utils/Log.h" 20 | 21 | #include 22 | 23 | 24 | 25 | class BpHelloWorldClient: public android::BpInterface 26 | { 27 | public: 28 | BpHelloWorldClient(const android::sp& impl) 29 | : android::BpInterface(impl) 30 | { 31 | } 32 | 33 | 34 | void hellothere(const char *str) 35 | { 36 | android::Parcel data, reply; 37 | data.writeInterfaceToken(IHelloWorldService::getInterfaceDescriptor()); 38 | data.writeCString(str); 39 | remote()->transact(HW_HELLOTHERE, data, &reply, android::IBinder::FLAG_ONEWAY); 40 | } 41 | 42 | }; 43 | 44 | android_IMPLEMENT_META_INTERFACE(HelloWorldClient, HELLOWORLD_NAME) 45 | -------------------------------------------------------------------------------- /libhelloworldservice/src/helloworldservice.cpp: -------------------------------------------------------------------------------- 1 | #define LOG_TAG "HelloWorldService" 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | #include "helloworldservice.h" 17 | #include "utils/Log.h" 18 | 19 | #include 20 | 21 | const android::String16 IHelloWorldService::descriptor(HELLOWORLD_NAME); 22 | 23 | android::String16 IHelloWorldService::getInterfaceDescriptor() { 24 | return IHelloWorldService::descriptor; 25 | } 26 | 27 | 28 | void HelloWorldService::instantiate() { 29 | android::defaultServiceManager()->addService( 30 | IHelloWorldService::descriptor, new HelloWorldService()); 31 | } 32 | 33 | HelloWorldService::HelloWorldService() 34 | { 35 | LOGE("HelloWorldService created"); 36 | mNextConnId = 1; 37 | } 38 | 39 | HelloWorldService::~HelloWorldService() 40 | { 41 | LOGE("HelloWorldService destroyed"); 42 | } 43 | 44 | #define CHECK_INTERFACE(interface, data, reply) \ 45 | do { if (!data.enforceInterface(interface::getInterfaceDescriptor())) { \ 46 | LOGW("Call incorrectly routed to " #interface); \ 47 | return android::PERMISSION_DENIED; \ 48 | } } while (0) 49 | 50 | android::status_t HelloWorldService::onTransact(uint32_t code, 51 | const android::Parcel &data, 52 | android::Parcel *reply, 53 | uint32_t flags) 54 | { 55 | LOGE("OnTransact(%u,%u)", code, flags); 56 | 57 | switch(code) { 58 | case HW_HELLOTHERE: { 59 | CHECK_INTERFACE(IHelloWorldService, data, reply); 60 | const char *str; 61 | str = data.readCString(); 62 | /* hellothere(str); */ 63 | LOGE("hello: %s\n", str); 64 | printf("hello: %s\n", str); 65 | return android::NO_ERROR; 66 | } break; 67 | default: 68 | return BBinder::onTransact(code, data, reply, flags); 69 | } 70 | 71 | return android::NO_ERROR; 72 | } 73 | 74 | -------------------------------------------------------------------------------- /res/drawable/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mcr/Android-HelloWorldService/b56d4e894d2b39ede61fddc939ddc325eea3f2c4/res/drawable/icon.png -------------------------------------------------------------------------------- /res/layout/hello.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | HelloWorldService 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/org/credil/helloworldservice/HelloWorld.java: -------------------------------------------------------------------------------- 1 | package org.credil.helloworldservice; 2 | 3 | import org.credil.helloworldservice.R; 4 | import android.app.Activity; 5 | import android.content.Context; 6 | import android.content.ComponentName; 7 | import android.content.Intent; 8 | import android.content.ServiceConnection; 9 | import android.os.Bundle; 10 | import android.os.IBinder; 11 | import android.os.RemoteException; 12 | import android.os.ServiceManager; 13 | import android.util.Log; 14 | import android.widget.TextView; 15 | 16 | public class HelloWorld extends Activity { 17 | 18 | private static final String LOG_TAG = "HelloWorld"; 19 | 20 | /* public HelloWorldServiceInterface hws; */ 21 | public TextView helloBox; 22 | public ServiceConnection serviceConnection; 23 | 24 | /** Called when the activity is first created. */ 25 | @Override 26 | public void onCreate(Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | setContentView(R.layout.hello); 29 | 30 | helloBox = (TextView)findViewById(R.id.HelloView01); 31 | helloBox.setText("start"); 32 | 33 | /* 34 | serviceConnection = new ServiceConnection() { 35 | public void onServiceConnected(ComponentName name, IBinder service) { 36 | Log.e("HelloWorld", "Service connected"); 37 | hws = HelloWorldServiceInterface.Stub.asInterface(service); 38 | if(hws != null) { 39 | helloBox.setText("connected"); 40 | Log.e("HelloWorld", "Connected"); 41 | } else { 42 | Log.e("HelloWorld", "Failed to connect"); 43 | } 44 | } 45 | 46 | public void onServiceDisconnected(ComponentName name) { 47 | Log.e("HelloWorld","disconnected"); 48 | hws = null; 49 | } 50 | }; 51 | 52 | // I think it should really be: 53 | //bindService(ServiceManager.getService("org.credilk..."), 54 | // serviceConnection, ???): 55 | 56 | bindService(new Intent("org.credil.helloworldservice.HelloWorldServiceInterface.START_SERVICE"), 57 | serviceConnection, Context.BIND_AUTO_CREATE); 58 | */ 59 | 60 | // BnServiceManager.getDefault(); 61 | String[] services; 62 | 63 | try { 64 | services = ServiceManager.listServices(); 65 | } catch (RemoteException e) { 66 | Log.e("HelloWorld", "No list of services."); 67 | return; 68 | } 69 | 70 | Log.e("HelloWorld", "services is "+(services.length)); 71 | int i; 72 | for(i=0; i