Android 使用AIDL实现两个app之间的通信
AIDL是基于Service完成app之间的通信,它是Android Interface definition language 的缩写,AIDL的通信是基于一个 .aidl 文件,需要两个app之间拥有同样的包名,然后同样的包名里面有同样的 .aidl 文件,这样才可以完成通信。
首先需要创建 .aidl 文件
(这是我两年前在eclipse上写的,那是AS还不是多普及,我先介绍下eclipse上的AIDL ,改天介绍AS上的,我先猜想一样,大概套路是一样的)
package com.example.aidlmy; //这个是包名,记得包名签名一定要加一个package
interface AidlDemo{ //AidlDemo 是 .aidl 文件的文件名
String name(); //里面定义的两个方法,记得另一个app也要有一个同样的包名,同样包名里面的这个文件
String age();
}
大家来看一下运行结果,可以看到这是客户端收到服务端的消息。
服务端:
下面是service类
import com.example.aidlmy.AidlDemo.Stub; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; public class MyService extends Service { private StudentTest student; @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); student=new StudentTest("小明", "13"); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return new MyBinder(); } class MyBinder extends Stub{ @Override public String name() throws RemoteException { // TODO Auto-generated method stub return student.getName(); } @Override public String age() throws RemoteException { // TODO Auto-generated method stub return student.getAge(); } } }
下面是一个Bean类,封装了两个参数,用来在另一个app里面获取
public class StudentTest { private String name; private String age; public StudentTest(String name, String age) { super(); this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
使用了service就要在配置清单注册,同时写入里面 .aidl文件的包名+.aidl
<service android:name="com.example.aidlmy.MyService"> <intent-filter> <action android:name="com.example.aidlmy.aidl"/> </intent-filter> </service>
客户端:
这是客户端布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.aidl_kehu.MainActivity" > <Button android:id="@+id/mBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="点击交互" /> </RelativeLayout>
客户端主要实现代码:
import com.example.aidlmy.AidlDemo; import com.example.myaidl.R; import android.app.Activity; import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Button mBtn; private AidlDemo binder; //客户端和服务端的实现,要定义ServiceConnection private ServiceConnection conn=new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub Log.e("", "连接失败"); } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub binder=AidlDemo.Stub.asInterface(service); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { mBtn=(Button) findViewById(R.id.mBtn); Intent intent=new Intent(); intent.setAction("com.example.aidlmy.aidl"); intent.setPackage("com.example.aidlmy"); //bindService建立连接 bindService(intent, conn,Service.BIND_AUTO_CREATE); mBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { String age = binder.age(); String name = binder.name(); Toast.makeText(MainActivity.this, "age"+age+"name"+name, Toast.LENGTH_LONG).show(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } }