Android
AIDL
Zoeken…
Invoering
AIDL is een Android-interfacedefinitietaal.
Wat? Waarom? Hoe ?
Wat? Het is een beperkte dienstverlening. Deze AIDL-service is actief totdat er ten minste één van de client bestaat. Het werkt op basis van marshaling en unmarshaling concept.
Waarom? Externe toepassingen hebben toegang tot uw service + Multi Threading. (Aanvraag externe toepassing).
Hoe? Maak het .aidl-bestand Implementeer de interface Stel de interface bloot aan clients
AIDL-service
ICalculator.aidl
// Declare any non-default types here with import statements
interface ICalculator {
int add(int x,int y);
int sub(int x,int y);
}
AidlService.java
public class AidlService extends Service {
private static final String TAG = "AIDLServiceLogs";
private static final String className = " AidlService";
public AidlService() {
Log.i(TAG, className+" Constructor");
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
Log.i(TAG, className+" onBind");
return iCalculator.asBinder();
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, className+" onCreate");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, className+" onDestroy");
}
ICalculator.Stub iCalculator = new ICalculator.Stub() {
@Override
public int add(int x, int y) throws RemoteException {
Log.i(TAG, className+" add Thread Name: "+Thread.currentThread().getName());
int z = x+y;
return z;
}
@Override
public int sub(int x, int y) throws RemoteException {
Log.i(TAG, className+" add Thread Name: "+Thread.currentThread().getName());
int z = x-y;
return z;
}
};
}
Serviceverbinding
// Return the stub as interface
ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, className + " onServiceConnected");
iCalculator = ICalculator.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
unbindService(serviceConnection);
}
};
Modified text is an extract of the original Stack Overflow Documentation
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow