In the above article, we have implemented the use of service to design Hal. However, according to the android framework, generally, applications do not directly communicate with the service, but need to go through a manager layer in the middle.
We will further optimize the Hal Design Based on the android framework here. The structure is as follows:
Hal stub <-> JNI library <-> JAVA service <-> Manager <-> JAVA application.
Because ledmanager and ledservice belong to two processes, the communication between them needs to be implemented through aidl. below is iledservice. aidl
1. package mokoid. hardware; <br/> 2. <br/> 3. interface iledservice <br/> 4. {<br/> 5. boolean Seton (INT led); <br/> 6. boolean setoff (INT led); <br/> 7 .}
This file defines the interfaces provided by the ledservice to the ledmanager. After the aidl tool is compiled into the iledservice. Java file.
Let's look at how ledmanager is implemented:
1. public class ledmanager <br/> 2. {<br/> 3. private Static final string tag = "ledmanager"; <br/> 4. private iledservice mledservice; <br/> 5. <br/> 6. public ledmanager () {<br/> 7. <br/> 8. mledservice = iledservice. stub. asinterface (<br/> 9. servicemanager. getservice ("led"); <br/> 10. <br/> 11. if (mledservice! = NULL) {<br/> 12. log. I (TAG, "The ledmanager object is ready. "); <br/> 13 .} <br/> 14 .} <br/> 15. <br/> 16. public Boolean ledon (int n) {<br/> 17. boolean result = false; <br/> 18. <br/> 19. try {<br/> 20. result = mledservice. seton (n); <br/> 21 .} catch (RemoteException e) {<br/> 22. log. E (TAG, "RemoteException in ledmanager. ledon: ", e); <br/> 23 .} <br/> 24. return result; <br/> 25 .} <br/> 26. <br/> 27. public Boolean ledoff (int n) {<br/> 28. boolean result = false; <br/> 29. <br/> 30. try {<br/> 31. result = mledservice. setoff (n); <br/> 32 .} catch (RemoteException e) {<br/> 33. log. E (TAG, "RemoteException in ledmanager. ledoff: ", e); <br/> 34 .} <br/> 35. return result; <br/> 36 .} <br/> 37 .}
Here, you need to note that mledservice = iledservice. stub. asinterface (nager. getservice ("led"); through this function, you can get the ledservice instance and call its method.
For ledservice, we need to make a small change, as shown below:
1. public final class ledservice extends iledservice. stub {<br/> 2. <br/> 3. static {<br/> 4. system. load ("/system/lib/libmokoid_runtime.so"); <br/> 5 .} <br/> 6. <br/> 7. public ledservice () {<br/> 8. log. I ("ledservice", "Go to get led stub... "); <br/> 9. _ Init (); <br/> 10 .} <br/> 11. <br/> 12. /* <br/> 13. * mokoid led native methods. <br/> 14. */<br/> 15. public Boolean Seton (INT led) {<br/> 16. log. I ("mokoidplatform", "led on"); <br/> 17. return _ set_on (LED); <br/> 18 .} <br/> 19. <br/> 20. public Boolean setoff (INT led) {<br/> 21. log. I ("mokoidplatform", "led off"); <br/> 22. return _ set_off (LED); <br/> 23 .} <br/> 24. <br/> 25. private Static native Boolean _ Init (); <br/> 26. private Static native Boolean _ set_on (INT led); <br/> 27. private Static native Boolean _ set_off (INT led); <br/> 28 .}
The ledservice must inherit from iledservice. stub to implement communication between the aidl interface and ledmanager.
Finally, let's look at the application. In the application, we implement a service (note the difference with the above service) and an activity, in
The ledservice is started in the service, and the interfaces provided by the ledservice are used in the activity through the ledmanager.
First, let's take a look at the service implementation:
1. public class ledsystemserver extends Service {<br/> 2. <br/> 3. @ override <br/> 4. public ibinder onbind (intent) {<br/> 5. return NULL; <br/> 6 .} <br/> 7. <br/> 8. public void onstart (intent, int startid) {<br/> 9. log. I ("ledsystemserver", "Start ledservice... "); <br/> 10. <br/> 11. /* Please also see systemserver. java for your interests. */<br/> 12. ledservice ls = new ledservice (); <br/> 13. <br/> 14. try {<br/> 15. servicemanager. addservice ("led", ls); <br/> 16 .} catch (runtimeexception e) {<br/> 17. log. E ("ledsystemserver", "Start ledservice failed. "); <br/> 18 .} <br/> 19 .} <br/> 20 .}
Note that servicemanager. addservice ("led", ls) can be used to start the ledservice created on the right side.
Let's look at the implementation of activity:
1. public class ledtest extends activity implements view. onclicklistener {<br/> 2. private ledmanager mledmanager = NULL; <br/> 3. <br/> 4. @ override <br/> 5. public void oncreate (bundle savedinstancestate) {<br/> 6. super. oncreate (savedinstancestate); <br/> 7. <br/> 8. // start ledservice in a seperated process. <br/> 9. startservice (new intent ("com. mokoid. systemserver "); <br/> 10. <br/> 11. bu Tton BTN = new button (this); <br/> 12. BTN. settext ("click to turn led 1 on"); <br/> 13. BTN. setonclicklistener (this); <br/> 14. <br/> 15. setcontentview (BTN); <br/> 16 .} <br/> 17. <br/> 18. public void onclick (view v) {<br/> 19. <br/> 20. // get ledmanager. <br/> 21. if (mledmanager = NULL) {<br/> 22. log. I ("ledtest", "creat a new ledmanager object. "); <br/> 23. mledmanager = new ledmanager (); <Br/> 24. }< br/> 25. <br/> 26. If (mledmanager! = NULL) {<br/> 27. log. I ("ledtest", "Got ledmanager object. "); <br/> 28 .} <br/> 29. <br/> 30. /** call methods in ledservice via proxy object <br/> 31. * which is provided by ledmanager. <br/> 32. */<br/> 33. mledmanager. ledon (1); <br/> 34. <br/> 35. textview TV = new textview (this); <br/> 36. TV. settext ("led 1 is on. "); <br/> 37. setcontentview (TV); <br/> 38 .} <br/> 39 .}
When this activity is created, the ledsystemserver defined above will be enabled through intent, and this ledsystemserver will enable the previous definition
When you click the button, a new ledmanager will be created, which will pass
Servicemanager. addservice ("led ",
Ls) to obtain the ledservice instance and provide the interface in the ledservice for this activity, so as to implement led control in this activity
.
This article is from "mobile and Linux deve ..
Blog, be sure to keep this source http://buaadallas.blog.51cto.com/399160/384919