communicate with service on android
2009-12-09 08:50
441 查看
While doing android programming, it's a common sense to use Activity to interact with users and use Service to perform time consuming tasks (actually, such tasks are usually performed on a new thread spawned by the service). A classic example is media player. It's composed of several Activities and a playing-back Service. Through Activities, users can pick a media file to play, or control (start, pause, fast forward, set volume, etc) the playing-back Service. And the Service is responsible for playing the media file. The service can run in background even no Activity of the player application is active. It's also possible that the Service is implemented and hosted in another application other than the one where Activities are implemented.
In order to allow Activities to control the behavior of the Service, there should be an mechanism to allow they talk to each other. And by following the Process Agnostic feature of Android platform, this communication mechanism should also be able to handle Inter-Process and Inside-Process communication consistently. Such that from the upper layer application code's point of view, it's making communications between components independent of which process these components run in.
Luckily, Android has already provided such a RPC mechanism which has been briefly introduced here: Android application fundamentails - remote procedure calls. And the document Designing a Remote Interface Using AIDL also shows a tutorial about how to perform RPC.
But the documents above don't include a complete yet simple example for us to follow, so I'd like to explore how to do Inside-Process as well as Inter-Process with an example.
This example is composed of two applications. The first application (app1) will bind to a service runs in another application (app2) when a button is clicked, thus demonstrates Inter-Process communication. app2 can bind to the service too, which demonstrates Inside-Process communication.
There are two buttons on each application's Activity. First, you need to click button titled start svc to bind to service and get a reference to Binder object. Then, you can click button titled call rpc to invoke a method in service.
It's trivial to repeat the tutorial of designing remote interface. So I'd like to just post some findings during my exploration.
The onBind method of service should return a non-null IBinder. Otherwise, the callback function onServiceConnected won't be fired.
Add two aidl interface with the same name to both projects (ISvcController.aidl in our example). And the interface can be used in both projects to manipulate the IBinder object returned by the service. These two interfaces can have different member functions, but any function to be used must have the same signature and be placed at the same place (index) in these interfaces.
When access the service in the same process, the original IBinder returned in onBind method is passed to onServiceConnected callback method. When access the service in a different process, a proxy for the IBinder is passed to onServiceConnected callback method.
It's necessary to call unbindService to avoid connection leaking. This can be done upon specific event handler such as a button click. A better choice is to unbind in one of Activity's life cycle events, for example, onStop. So that the connection will be freed no matter in which way the user leaves the Activity.
Source Code
browse code here:
http://code.google.com/p/rxwen-blog-stuff/source/browse/#svn/trunk/android/rpc-with-service
or via command:
svn co https://rxwen-blog-stuff.googlecode.com/svn/trunk/android/rpc-with-service href="https://rxwen-blog-stuff.googlecode.com/svn/trunk/android/rpc-with-service" target=_blank>
In order to allow Activities to control the behavior of the Service, there should be an mechanism to allow they talk to each other. And by following the Process Agnostic feature of Android platform, this communication mechanism should also be able to handle Inter-Process and Inside-Process communication consistently. Such that from the upper layer application code's point of view, it's making communications between components independent of which process these components run in.
Luckily, Android has already provided such a RPC mechanism which has been briefly introduced here: Android application fundamentails - remote procedure calls. And the document Designing a Remote Interface Using AIDL also shows a tutorial about how to perform RPC.
But the documents above don't include a complete yet simple example for us to follow, so I'd like to explore how to do Inside-Process as well as Inter-Process with an example.
This example is composed of two applications. The first application (app1) will bind to a service runs in another application (app2) when a button is clicked, thus demonstrates Inter-Process communication. app2 can bind to the service too, which demonstrates Inside-Process communication.
There are two buttons on each application's Activity. First, you need to click button titled start svc to bind to service and get a reference to Binder object. Then, you can click button titled call rpc to invoke a method in service.
It's trivial to repeat the tutorial of designing remote interface. So I'd like to just post some findings during my exploration.
The onBind method of service should return a non-null IBinder. Otherwise, the callback function onServiceConnected won't be fired.
Add two aidl interface with the same name to both projects (ISvcController.aidl in our example). And the interface can be used in both projects to manipulate the IBinder object returned by the service. These two interfaces can have different member functions, but any function to be used must have the same signature and be placed at the same place (index) in these interfaces.
When access the service in the same process, the original IBinder returned in onBind method is passed to onServiceConnected callback method. When access the service in a different process, a proxy for the IBinder is passed to onServiceConnected callback method.
It's necessary to call unbindService to avoid connection leaking. This can be done upon specific event handler such as a button click. A better choice is to unbind in one of Activity's life cycle events, for example, onStop. So that the connection will be freed no matter in which way the user leaves the Activity.
Source Code
browse code here:
http://code.google.com/p/rxwen-blog-stuff/source/browse/#svn/trunk/android/rpc-with-service
or via command:
svn co https://rxwen-blog-stuff.googlecode.com/svn/trunk/android/rpc-with-service href="https://rxwen-blog-stuff.googlecode.com/svn/trunk/android/rpc-with-service" target=_blank>
相关文章推荐
- communicate with service on android
- 【android】在Service的onStartCommand()中调用stopself()应该注意的问题
- Android ServiceConnection类的onServiceDisconnected(ComponentName name)在什么时候执行
- Ways of Finding element on android with Appium
- NoClassDefFoundError with Android Studio on Android 4
- have error with saxParser on android(not well-formed (invalid token))问题解决
- Android Service生命周期 Service里面的onStartCommand()方法详解
- Android Service生命周期 Service里面的onStartCommand()方法详解
- eclipse with android : Didn't find class "com.xx.xx.MainActivity" on path: DexPathList
- android in practice_Communicating with a Service (portfolio project)
- Hosting Multiple Service Implementations On The Same Port With WCF
- android在Service,BroadCast onReceiver()中弹出Dialog对话框,即全局性对话框
- Android Service 中 onStartCommand()函数返回值含义
- Android中service的onStartCommand()方法中intent为null的问题
- Conversion to Dalvik format failed with error 1 in Android on export
- [Android] Fastest Way to Get on Track with Android Development
- 我的Android进阶之旅------>android中service的onStartCommand()方法中intent为null的问题
- Programming 3D games on Android with Irrlicht and Bullet
- IPC analysis on android with a demo (基于IPC实例分析android IPC机制)
- Android Service 的 onStartCommand 返回值