一,AIDL详情
AIDL是一种定义接口语言,用于多个应用间使用同一个Service的功能。
相比其他跨线程通信:Broadcast Receiver,它的优势是:性能稳定,效率高,而且系统上实现共享内存。
二,AIDL的使用:
通常使用它需要启动Client端和Service端
Service端创建
1,先创建AIDL
2,修改aidl文件,并 Rebuild
interface IMyAidlInterface {
//传入int数据,返回string 数据
String getstring(int data);
}
3,创建service
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
return iMyAidlInterface;
}
private IMyAidlInterface.Stub iMyAidlInterface = new IMyAidlInterface.Stub() {
@Override
public String getstring(int data) throws RemoteException {
String str = "传进来的数字:"+data;
return str;
}
};
}
Client 客户端创建
private IMyAidlInterface iMyAidlInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 绑定AIDL
*/
public void onbang(View view ){
Intent intent = new Intent(this , MyService.class);
bindService(intent , connect , BIND_AUTO_CREATE);
}
/**
* 解绑
*/
public void unbang(View view){
unbindService(connect);
Toast.makeText(MainActivity.this , "解除绑定" , Toast.LENGTH_LONG).show();
}
/**
* 连接
*/
private ServiceConnection connect = new ServiceConnection() {
/**
* 连接成功
* @param name
* @param service
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
try {
String str = iMyAidlInterface.getstring(1);
Toast.makeText(MainActivity.this , str , Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
/**
* 连接失败
* @param name
*/
@Override
public void onServiceDisconnected(ComponentName name) {
iMyAidlInterface = null;
}
};
}
显示如图
第三方应用调用该服务
这里和之前的不一样
1,把之前的ADIL文件夹复制到第三方应用
2,调取的时候发生改变,创建Componnent
public void onbang(View view ){
Intent intent = new Intent();
ComponentName com = new ComponentName(
"qzjiami.c", //包名
"qzjiami.c.MyService");//服务类
intent.setComponent(com);
bindService(intent , connect , BIND_AUTO_CREATE);
}
主代码:
public class MainActivity extends AppCompatActivity {
private IMyAidlInterface DiMyAidlInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 绑定AIDL
*/
public void onbang(View view ){
Intent intent = new Intent();
ComponentName com = new ComponentName(
"qzjiami.c", //包名
"qzjiami.c.MyService");//服务类
intent.setComponent(com);
bindService(intent , connect , BIND_AUTO_CREATE);
}
/**
* 解绑
*/
public void unbang(View view){
unbindService(connect);
Toast.makeText(MainActivity.this , "解除绑定" , Toast.LENGTH_LONG).show();
}
/**
* 连接
*/
private ServiceConnection connect = new ServiceConnection() {
/**
* 连接成功
* @param name
* @param service
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
DiMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
try {
String str = DiMyAidlInterface.getstring(2);
Toast.makeText(MainActivity.this , str , Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
/**
* 连接失败
* @param name
*/
@Override
public void onServiceDisconnected(ComponentName name) {
DiMyAidlInterface = null;
}
};
}
显示如图: