Android uses the AIDL mechanism to call remote services. androidaidl

Source: Internet
Author: User
Tags call back

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.