In the interface file AIDLFunctions.aidl, we define a method show
interface AidlFunctions{
void show();
}
Server:
The use of AIDL requires a Service, so we also declare a Service on the server
public class AIDLService extends Service { //stub is AidlFunctions.Stub binder
automatically generated by the system ; @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); } @Override public IBinder onBind(Intent intent) { // TODO Auto -generated method stub binder = new AidlFunctions.Stub() { @Override //Here is the implementation of the method we declared in the interface
public void show() throws RemoteException {
// TODO Auto-generated method stub
System.out.println("--------------------收到----------------------");
}
};
return binder;
}
}
在AndroidManifest声明Service
<service android:name="com.example.androidaidl.AIDLService">
<intent-filter>
<action android:name="com.example.androidaidl.AIDLService" />
</intent-filter>
</service>
客户端:
//绑定服务,要用到ServiceConnection
private ServiceConnection serviceConnection;
//Custom interface, same as server
private AidlFunctions aidlFunctions;
serviceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
System.out.println("--------------------ServiceDisconnected----------------------");
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
System.out.println("--------------------ServiceConnected----------------------");
aidlFunctions = AidlFunctions.Stub.asInterface(service);
}
};
Intent intent = new Intent("com.example.androidaidl.AIDLService");
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
//调用show方法
try {
aidlFunctions.show();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
AIDL
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326180950&siteId=291194637
Ranking