AIDL说明
AIDL是Android中IPC(Inter-Process Communication)方式中的一种,AIDL是Android Interface definition language的缩写,对于小白来说,AIDL的作用是让你可以在自己的APP里绑定一个其他APP的service,这样你的这个应用可以和其他应用交互,也就是应用之间的通信
AIDL的用法
首先创建服务端,我这里创建一个名为appservice的Module 右键创建aidl文件如下图
我这里就用系统默认生成的名字IMyAidlInterface(可以自己随意命名)
// IMyAidlInterface.aidl
package t.s.com.appservice;
// Declare any non-default types here with import statements
interface IMyAidlInterface {
String getMsg();//我自己定义的方法
/**
* 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);//basicTypes这个方法可以无视,看注解知道这个方法只是告诉你在AIDL中你可以使用的基本类型(int, long, boolean, float, double, String)
}
定义好之后,就可以sycn project一下,然后新建一个service。在service里面创建一个内部类,继承你刚才创建的AIDL的名称里的Stub类,并实现接口方法,在onBind返回内部类的实例。
package t.s.com.appservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class MyService extends Service {
public void onCreate() {
super.onCreate();
Log.i("tttt","onCreate");
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return myAidlInterface;
}
private final IMyAidlInterface.Stub myAidlInterface= new IMyAidlInterface.Stub() {
@Override
public String getMsg() throws RemoteException {
Log.i("tttt","getMsg");
String msg="我的AIDL";
return msg;
}
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
Log.i("tttt","basicTypes");
}
};
}
记得在Mainifest.xml注册这个service
<service
android:name=".MyService">
<intent-filter>
<action android:name="t.s.com.appservice" />
</intent-filter>
</service>
到此整个服务端的就创建完毕!
下面看下客服端如何调用
将我们刚刚服务端的AIDL文件拷贝到第二个项目(注意两个aidl文件所在路径要完全一样,看下图)
然后sycn project一下工程。
然后在Activity中绑定服务
package t.s.com;
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.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import butterknife.ButterKnife;
import butterknife.OnClick;
import t.s.com.appservice.IMyAidlInterface;
import t.s.com.desgin.DesignActivity;
import t.s.com.desgin.DrawerLayoutActivity;
import t.s.com.javaee.ProductList;
import t.s.com.javaee.ProductTwo;
import t.s.com.javaee.Productone;
public class MainActivity extends AppCompatActivity {
private IMyAidlInterface maidl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
ProductList productList=ProductList.getInstance();
Productone pone=new Productone();
ProductTwo ptwo=new ProductTwo();
productList.addObserver(pone);
productList.addObserver(ptwo);
productList.addProuduct("新进产品");
}
@OnClick({R.id.one, R.id.two, R.id.three,R.id.aidls,R.id.detail})
void onclick(View v) {
switch (v.getId()) {
case R.id.one:
startActivity(new Intent(MainActivity.this, DrawerLayoutActivity.class));
break;
case R.id.two:
startActivity(new Intent(MainActivity.this, DesignActivity.class));
break;
case R.id.three:
startActivity(new Intent(MainActivity.this, DesignActivity.class));
break;
case R.id.aidls://绑定
buttonClick();
break;
case R.id.detail://调用方法
if(maidl==null){
buttonClick();
}else {
String sd="";
try {
sd=maidl.getMsg();
} catch (RemoteException e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this,"lll="+sd ,Toast.LENGTH_SHORT).show();
}
break;
}
}
/**
* 监听按钮点击
* @param
*/
public void buttonClick() {
Log.i("tttt","buttonClick=");
Intent intent = new Intent();
intent.setComponent(new ComponentName("t.s.com.appservice", "t.s.com.appservice.MyService"));//(服务端service所在包名,服务端service全路径)
bindService(intent, conn, Context.BIND_AUTO_CREATE);
}
ServiceConnection conn = new ServiceConnection(){
@Override
public void onServiceDisconnected(ComponentName name){
}
@Override
public void onServiceConnected(ComponentName name, IBinder service){
maidl = IMyAidlInterface.Stub.asInterface(service);
try {
Toast.makeText(MainActivity.this,"绑定成功" ,Toast.LENGTH_SHORT).show();
maidl.basicTypes(12, 1332, true, 12.2f, 12.3, "测试测试");
} catch (RemoteException e) {
e.printStackTrace();
}
Log.i("tttt",maidl.toString());
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(conn);
}
}
到此,AIDL的使用方法就完成了
原码地址请点击