Android uses the AIDL mechanism to call remote services. androidaidl
Server: // CalculateInterface. aidlpackage com. itheima. aidl. calculate; interface CalculateInterface {double doCalculate (double a, double B);} // CalculateService. javapackage com. itheima. myaidl. server; import com. itheima. aidl. calculate. calculateInterface; import android. app. service; import android. content. intent; import android. OS. IBinder; import android. OS. remoteException; import android. util. log; public class CalculateService extends Service {private final CalculateInterface. stub mBinder = new CalculateInterface. stub () {@ Overridepublic double doCalculate (double a, double B) throws RemoteException {return a + B ;};@ Overridepublic IBinder onBind (Intent intent) {Log. I ("test", "onBind... "); return mBinder ;}@ Overridepublic boolean onUnbind (Intent intent) {Log. I ("test", "onUnbind... "); return super. onUnbind (intent) ;}@ Overridepublic void onCreate () {super. onCreate (); Log. I ("test", "onCreate... ") ;}@ Overridepublic void onDestroy () {super. onDestroy (); Log. I ("test", "onDestroy... "); }}// server manifast file <manifest xmlns: android =" http://schemas.android.com/apk/res/android "package =" com. itheima. myaidl. server "android: versionCode =" 1 "android: versionName =" 1.0 "> <uses-sdk android: minSdkVersion =" 14 "android: targetSdkVersion = "19"/> <application android: allowBackup = "true" android: icon = "@ drawable/ic_launcher" android: label = "@ string/app_name" android: theme = "@ style/AppTheme"> <activity android: name = "com. itheima. myaidl. server. mainActivity "android: configChanges =" locale | layoutDirection "android: theme =" @ android: style/Theme. light. noTitleBar "android: screenOrientation =" portrait "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> <service android: name =" com. itheima. myaidl. server. calculateService "> <intent-filter> <action android: name =" com. itheima. myaidl. server. calculateService "/> </intent-filter> </service> </application> </manifest> // client // MainActivity. javapackage com. itheima. myaidl. client; 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 com. itheima. aidl. calculate. calculateInterface; public class MainActivity extends Activity {private CalculateInterface mService; private ServiceConnection mServiceConnection = new ServiceConnection () {@ Override public void onServiceDisconnected (ComponentName name) {Log. I ("test", "service disconnected... "); mService = null ;}@ Override public void onServiceConnected (ComponentName, IBinder service) {Log. I ("test", "service connected... "); mService = CalculateInterface. stub. asInterface (service); // obtain interface instance }}; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // bind the remote service Bundle bundle = new Bundle (); Intent intent = new Intent (); intent. putExtras (bundle); intent. setAction ("com. itheima. myaidl. server. calculateService "); bindService (intent, mServiceConnection, Context. BIND_AUTO_CREATE);} // call back this method when the TODO activity is loaded @ Override public void onWindowFocusChanged (boolean hasFocus) {if (hasFocus) {try {double result = mService. doCalculate (1, 2); Log. I ("test", "result =>" + result);} catch (RemoteException e) {e. printStackTrace () ;}} super. onWindowFocusChanged (hasFocus);} @ Override protected void onDestroy () {unbindService (mServiceConnection); // unbind remote service super. onDestroy ();}}
Running result: