AIDL:Android 语言的界面定义 (AIDL) 其他与你可能使用过的接口语言 (IDL) 类似。您可以使用它来定义客户端和服务认可的编程界面,以便在使用过程中进行通信 (IPC) 互相通信。在 Android 一个过程通常无法访问另一个过程的内存。因此,为了进行通信,过程需要将其对象分解成操作系统可以理解的原语,并将其组织成可供您操作的对象。编写和执行编组操作的代码比较繁琐,所以 Android 会使用 AIDL 为您解决这个问题。
注:只有在需要不同应用程序的客户端通过 IPC 只有在服务中进行多线程处理时,才需要使用方式访问服务 AIDL。如果您不需要跨不同的应用程序并发执行 IPC,它应该通过实现 Binder 创建界面;或者,如果你想执行它 IPC,但不需要处理多线程,请使用 Messenger 实现接口。无论如何,都在实现 AIDL 以前,请务必了解绑定服务。
您可以通过 IPC 接口将某一类从一个过程发送到另一个过程。然而,您必须确保 IPC 这种代码可以在通道的另一端使用,必须支持 Parcelable 接口。支持 Parcelable 接口很重要,因为 Android 该系统可以通过接口将对象分解成可编组到每个过程的原始语言。本文介绍在第二部分.
代码结构 demoaidl文件:
// IMyAidlInterface.aidlpackage com.example.buringteng.myapplication;// Declare any non-default types here with import statementsinterface 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);}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.buringteng.myapplication"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".RemoteService" android:enabled="true" android:exported="true" > </service> </application></manifest>
MainActivity.java
public class MainActivity extends AppCompatActivity { //The interface will be calling in service IMyAidlInterface iRemoteService = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); Intent intent = new Intent(MainActivity.this,RemoteService.class); intent.setAction(IMyAidlInterface.class.getName()); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); } }); } private ServiceConnection mConnection = new ServiceConnection() { // Called when the connection with the service is established public void onServiceConnected(ComponentName className, IBinder service) { Log.e("aidl test", "Service connected"); // Following the example above for an AIDL interface, // this gets an instance of the IMyAidlInterface, which we can use to call on the service iRemoteService = IMyAidlInterface.Stub.asInterface(service); // iRemoteService.basicType; try { iRemoteService.basicTypes(1, 2L,false, 7.72f, 6.77f, "Hello world"); } catch (RemoteException e) { e.printStackTrace(); } } // Called when the connection with the service disconnects unexpectedly public void onServiceDisconnected(ComponentName className) { Log.e("aidl test", "Service has unexpectedly disconnected"); iRemoteService = null; } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
RemoteService.java
public class RemoteService extends Service { @Override public void onCreate() { super.onCreate(); } @Override public IBinder onBind(Intent intent) { // Return the interface Log.e("onBind", "invoke"); return binder; } private final IMyAidlInterface.Stub binder = new IMyAidlInterface.Stub() { public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString){ Log.e("Stub test", ""+anInt); } };}
补充
<service android:name=".RemoteService" android:enabled="true" android:exported="true" >
android:enable 执行service是否可以实例化 android.exported 用于制定其他应用程序组件是否可以调用service或与其交互.true表示能,false表示不能.