這篇文章主要講解了Android中如何使用aidl,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
AIDL是Android中IPC(Inter-Process Communication)方式中的一種,AIDL是Android Interface definition language的縮寫(對于小白來說,AIDL的作用是讓你可以在自己的APP里綁定一個其他APP的service,這樣你的APP可以和其他APP交互。)
AIDL只是Android中眾多進程間通訊方式中的一種方式,
AIDL和Messenger的區別:
AIDL通信的原理:首先看這個文件有一個叫做proxy的類,這是一個代理類,這個類運行在客戶端中,其實AIDL實現的進程間的通信并不是直接的通信,客戶端和服務端都是通過proxy來進行通信的:客戶端調用的方法實際是調用是proxy中的方法,然后proxy通過和服務端通信將返回的結果返回給客戶端。
1、AIDL的作用
AIDL是用于Android的IPC通訊的,因此可以在一個APP內部通訊,也可以創建兩個APP之間進行通訊。
AIDL的職能分配很明確,Service作為后臺運行作為服務器管理各種交互,Client作為客戶端請求數據或調用Service的方法。
2、AIDL的簡單使用
1)創建一個aidl文件,直接右鍵創建就可以了,
package com.example.mytest;
// IMyAidlInterface.aidl package com.example.mytest; // Declare any non-default types here with import statements interface IMyAidlInterface { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString); String add(int x , int y); }
2)選中剛剛建立的 .aidl文件 生產對應的java文件。
AndroidStudio 可以通過Build--》model App 完成
3)編寫Service的具體對象 實現接口
package com.example.mytest; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; public class FirstService extends Service { public FirstService() { } private static String Tag = "FirstService"; @Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. //throw new UnsupportedOperationException("Not yet implemented"); Log.d(Tag,"service on bind"); return mBinder; } @Override public void onCreate() { super.onCreate(); Log.d(Tag,"OnCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d(Tag,"onStartCommand"); return START_STICKY; } @Override public boolean onUnbind(Intent intent) { Log.d(Tag,"onUnbind"); return super.onUnbind(intent); } @Override public void onDestroy() { super.onDestroy(); Log.d(Tag,"onDestroy"); } IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub(){ @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } @Override public String add(int x, int y) throws RemoteException { Log.d(Tag,x + "--" + y); return String.valueOf(x + y); } }; }
注意:onBund 返回IBinder類型,為了后面的回調會調動
4)確定一下AndroidManifest.xml里面的Service內容
<service android:name=".FirstService" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="com.example.mytest.aidl.FirstService"/> </intent-filter> </service>
5)打開服務
private Button btnStartService; private Button btnBindService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { tvId = (TextView) findViewById(R.id.tv_id); btnStartService = (Button) findViewById(R.id.btn_Start_Service); btnStartService.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(); intent.setPackage("com.example.mytest"); intent.setAction("com.example.mytest.aidl.FirstService"); startService(intent); } }); btnBindService = (Button) findViewById(R.id.btnBindService); btnBindService.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { bind(); } }); private void bind(){ Log.d(Tag, "bind"); Intent intent = new Intent(); intent.setPackage("com.example.mytest"); if(controllerConnection != null){ this.bindService(intent,controllerConnection,this.BIND_AUTO_CREATE);//綁定服務,建立鏈接 } else { Log.d(Tag, "controllerConnection != null"); } } private void unbind(){ if(controllerConnection != null && myAIDLController.asBinder().isBinderAlive()){ try{ Log.d(Tag, "this.unbindService(controllerConnection);"); this.unbindService(controllerConnection); } catch (Exception localException) { Log.w(Tag, "unbind Exception localException"); } } }
在bind的時候是異步的,因此可以通過onServiceConnected()來判斷綁定上后的操作。
private ServiceConnection controllerConnection = new ServiceConnection(){ @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { Log.d(Tag,"onServiceConnected"); myAIDLController = IMyAidlInterface.Stub.asInterface(iBinder); if (myAIDLController != null) { try { Log.d(Tag, "ServiceConnection success"); Toast.makeText(MainActivity.this, "ServiceConnection success", Toast.LENGTH_LONG).show(); } catch (Exception localException) { Log.w(Tag, "Exception localException"); } } } @Override public void onServiceDisconnected(ComponentName componentName) { Log.d(Tag,"onServiceDisconnected"); Toast.makeText(MainActivity.this, "onServiceDisconnected", Toast.LENGTH_LONG).show(); myAIDLController = null; } @Override public void onBindingDied(ComponentName name) { Log.d(Tag,"onBindingDied"); } @Override public void onNullBinding(ComponentName name) { Log.d(Tag,"onNullBinding"); } };
6)調用Service方法add()
調用的時候需要綁定Service方法,上面已經有了,接下來調用就簡單了,創建一個Button,然后拿到Service的控制對象,調用方法add
btnServiceFunc = (Button) findViewById(R.id.btnServiceFunc); btnServiceFunc.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { Log.d(Tag, String.valueOf( myAIDLController.add(1,2))); } catch (RemoteException e) { e.printStackTrace(); } } });
看完上述內容,是不是對Android中如何使用aidl有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。