详解Androidaidl的使⽤⽅法
AIDL是Android中IPC(Inter-Process Communication)⽅式中的⼀种,AIDL是Android Interface definition language的缩写(对于⼩⽩来说,AIDL的作⽤是让你可以在⾃⼰的APP⾥绑定⼀个其他APP的rvice,这样你的APP可以和其他APP交互。)
AIDL只是Android中众多进程间通讯⽅式中的⼀种⽅式,
AIDL和Mesnger的区别:
1. Mesnger不适⽤⼤量并发的请求:Mesnger以串⾏的⽅式来处理客户端发来的消息,如果⼤量的消息同时发送到服
务端,服务端仍然只能⼀个个的去处理。
2. Mesnger主要是为了传递消息:对于需要跨进程调⽤服务端的⽅法,这种情景不适⽤Mesnger。
3. Mesnger的底层实现是AIDL,系统为我们做了封装从⽽⽅便上层的调⽤。
rapetube4. AIDL适⽤于⼤量并发的请求,以及涉及到服务端端⽅法调⽤的情况
AIDL通信的原理:⾸先看这个⽂件有⼀个叫做proxy的类,这是⼀个代理类,这个类运⾏在客户端中,其实AIDL实现的进程间的通信并不是直接的通信,客户端和服务端都是通过proxy来进⾏通信的:客户端调⽤的⽅法实际是调⽤是proxy中的⽅法,然后proxy通过和服务端通信将返回的结果返回给客户端。
1、AIDL的作⽤
AIDL是⽤于Android的IPC通讯的,因此可以在⼀个APP内部通讯,也可以创建两个APP之间进⾏通讯。
AIDL的职能分配很明确,Service作为后台运⾏作为服务器管理各种交互,Client作为客户端请求数据或调⽤Service的⽅法。
2、AIDL的简单使⽤
1)创建⼀个aidl⽂件,直接右键创建就可以了,
音标发音视频
st;
// IMyAidlInterface.aidl
st;
// Declare any non-default types here with import statements
interface IMyAidlInterface {
/**
* Demonstrates some basic types that you can u 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的具体对象实现接⼝
st;
import android.app.Service;
t.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 rvice.
//throw new UnsupportedOperationException("Not yet implemented");
Log.d(Tag,"rvice on bind");
return mBinder;
}
@Override
public void 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");
Unbind(intent);
}
@Override
fornicationpublic void onDestroy() {
Log.d(Tag,"onDestroy");
comparison}
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)确定⼀下l⾥⾯的Service内容
<rvice
android:name=".FirstService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="st.aidl.FirstService"/>
number ones
</intent-filter>英汉互译器
</rvice>
5)打开服务
private Button btnStartService;
private Button btnBindService;
@Override
protected void onCreate(Bundle savedInstanceState) {
tContentView(R.layout.activity_main);
initView();
}
private void initView() {
tvId = (TextView) findViewById(R.id.tv_id);
btnStartService = (Button) findViewById(R.id.btn_Start_Service);
btnStartService.tOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.tPackage("st");
intent.tAction("st.aidl.FirstService");
startService(intent);
}
});
btnBindService = (Button) findViewById(R.id.btnBindService);
btnBindService.tOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bind();
}
});
private void bind(){
Log.d(Tag, "bind");
Intent intent = new Intent();
intent.tPackage("st");
if(controllerConnection != null){
this.bindService(intent,controllerConnection,this.BIND_AUTO_CREATE);//绑定服务,建⽴链接
}
el {
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.tOnClickListener(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的使⽤⽅法的⽂章就介绍到这了,更多相关Android aidl的使⽤内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!