To build a remote (between different processes) service in Android, you must use aidl
I. Server:
1. aidl definition: After aidl definition, the compiler will automatically generate the aidl Java file in the r file.
package com.cjf.stock1;interface IStockQuoteService {double getQuote(String ticker);}
2. Implement the aidl Interface
Package COM. CJF. stock1; import android. app. service; import android. content. intent; import android. OS. ibinder; import android. OS. remoteException; import android. util. log; // service class inheritance must be servicepublic class stockquoteservice extends Service {Private Static final string tag = "stockquoteservice"; // implement aidl (istockquoteservice. stub) interface. This class acts as the implementation of remote services, and the onbind () method returns the public class stockquoteserviceimpl extends istockquoteservice of this class instance. stub {public double getquote (string ticker) throws RemoteException {log. I (TAG, "getquote () called for" + ticker); Return 20.0 ;}/// called after oncreate () @ overridepublic ibinder onbind (intent) {log. I (TAG, "onbind () called"); return New stockquoteserviceimpl (); // return an instance of aidl} // this method is called first when a service is bound, perform initialization (called every binding) @ overridepublic void oncreate () {super. oncreate (); log. I (TAG, "oncreate () called");} // when unbinding, this method will be called @ overridepublic void ondestroy () {log. I (TAG, "ondestory () called"); super. ondestroy ();}}
3. description file androidmanifest. xml Declaration
<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. CJF. stock1 "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <uses-SDK Android: minsdkversion =" 10 "/> <application Android: icon = "@ drawable/ic_launcher" Android: Label = "@ string/app_name"> <service android: Name = ". stockquoteservice "> <intent-filter> <! -- Add filter --> <action Android: Name = "com. CJF. stock1.istockquoteservice "/> </intent-filter> </service> </Application> </manifest>
Ii. Client
Call the service from a client application. Note that the aidl interface file of the server must be copied in the client project, and the package name must be consistent with that of the server.
1. Main. xml layout File
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <ToggleButton android:id="@+id/bindBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="Bind" android:textOff="Unbind" android:onClick="doClick" /> <Button android:id="@+id/callBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="call service" android:enabled="false" android:onClick="doClick"/></LinearLayout>
2, mainactivity
Package COM. CJF. client1; import COM. CJF. stock1.istockquoteservice; import android. app. activity; import android. content. componentname; import android. content. context; import android. content. intent; import android. content. serviceconnection; import android. OS. bundle; import android. OS. ibinder; import android. OS. remoteException; import android. util. log; import android. view. view; import android. widget. button; import android. widget. toast; import android. widget. togglebutton; // The client copies the aidl file of the server, and the package name of the aidl file must be the same as the public class mainactivity extends activity {Private Static final string tag = "mainactivity "; private istockquoteservice stockservice = NULL; private togglebutton bindbtn; private button callbtn; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); bindbtn = (togglebutton) This. findviewbyid (R. id. bindbtn); callbtn = (button) This. findviewbyid (R. id. callbtn);} public void doclick (view v) {Switch (v. GETID () {case R. id. bindbtn: If (togglebutton) V ). ischecked () {// belongs to the asynchronous call of bindservice (new intent (istockquoteservice. class. getname (), // The Name Of The aidl service serconn, // an instance context linked. bind_auto_create); // automatically create a service ID} else {unbindservice (serconn); callbtn. setenabled (false);} break; case R. id. callbtn: callservice (); // call the service break;} // call the service method private void callservice () {try {double val = stockservice. getquote ("android"); toast. maketext (this, "value from service is" + val, toast. length_long ). show ();} catch (RemoteException e) {// todo auto-generated catch blocke. printstacktrace () ;}// this object (service link object) Private serviceconnection serconn = new serviceconnection () needs to be used to bind and remove services () {// call Public void onserviceconnected (componentname, ibinder Service) {log. I (TAG, "onserviceconnected called"); stockservice = istockquoteservice. stub. asinterface (service); // obtain a reference to bindbtn in aidl. setchecked (true); // select callbtn. setenabled (true); // available status} // automatically call Public void onservicedisconnected (componentname name) {log. I (TAG, "onservicedisconnected called"); bindbtn. setchecked (false); // select callbtn. setenabled (false); // available status stockservice = NULL ;};@ overrideprotected void ondestroy () {log. I (TAG, "ondestroy () called"); If (callbtn. isenabled () {unbindservice (serconn);} super. ondestroy ();}}
When you click the BIND button: