編輯:關於android開發
服務端: //CalculateInterface.aidl package com.itheima.aidl.calculate; interface CalculateInterface { double doCalculate(double a, double b); } //CalculateService.java package 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() { @Override public double doCalculate(double a, double b) throws RemoteException { return a+b; } }; @Override public IBinder onBind(Intent intent) { Log.i("test","onBind..."); return mBinder; } @Override public boolean onUnbind(Intent intent) { Log.i("test","onUnbind..."); return super.onUnbind(intent); } @Override public void onCreate() { super.onCreate(); Log.i("test","onCreate..."); } @Override public void onDestroy() { super.onDestroy(); Log.i("test","onDestroy..."); } } //服務端manifast文件 <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> //客戶端 //MainActivity.java package 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 name, IBinder service) { Log.i("test","service connected..."); mService = CalculateInterface.Stub.asInterface(service); //獲取接口實例 } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //綁定遠程服務 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); } //TODO activity加載完畢時回調此方法 @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); //解綁遠程服務 super.onDestroy(); } }
運行結果截圖:
Android群英傳-拼圖游戲puzzle-6點吐槽 一、緣由 經常寫文章,混了一些C幣。最近在深入學習Android應用開發,就從商城裡買了一本《Android群英
安卓開源項目周報0104,安卓開源項目0104由OpenDigg 出品的安卓開源項目周報第四期來啦。我們的安卓開源周報集合了OpenDigg一周來新收錄的優質的
Android應用安全開發之淺談網頁打開APP,androidapp一、網頁打開APP簡介 Android有一個特性,可以通過點擊網頁內的某個鏈接打開APP,或者在其他A
android permission權限與安全機制解析(下) android 6.0權限全面詳細分析和解決方案 Marshmallow版本權限修改 androi
android Unable toexecute dex: method