Aidl && Service
0x10 onTransact方式连接
// 调用transact方法与bind上的service交互
button7.setText("ServiceDemo");
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mIBinder == null) {
Toast.makeText(mContext, "Connect server failed.", Toast.LENGTH_SHORT).show();
} else {
Parcel _data = Parcel.obtain();
Parcel _reply = Parcel.obtain();
String _result;
try {
_data.writeInterfaceToken("com.uodis.opendevice.aidl.OpenDeviceIdentifierService");
mIBinder.transact(1, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
output.setText("Get Oaid from Server: " + _result);
} catch (RemoteException e) {
e.printStackTrace();
} finally {
_reply.recycle();
_data.recycle();
}
}
}
});
// 自己实现一个ServiceConnection类
private IBinder mIBinder;
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "Service Connected: " + name.getClassName());
mIBinder = service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "Service DisConnected: " + name.getClassName());
}
};
// 在onCreate中bind Service
public void bindService() {
Intent service = new Intent();
service.setComponent(new ComponentName("com.huawei.hwid", "com.huawei.hms.opendeviceidentifier.OpenDeviceIdentifierBindService"));
service.setAction("com.uodis.opendevice.OPENIDS_SERVICE");
bindService(service, conn, Service.BIND_AUTO_CREATE);
}
0x20 通过aidl方式连接
-
在main目录下新建一个aidl目录:
- 并且新建一个aidl文件, 里面声明接口:
package com.uodis.opendevice.aidl;
/** 重要:请不要修改此AIDL文件的方法顺序 */
interface OpenDeviceIdentifierService {
/** 获取OAID */
String getOaid();
/** 获取限制跟踪参数,true:限制跟踪;false:不限制跟踪*/
boolean isOaidTrackLimited();
}
- 按Ctrl+F9可以重新编译项目工程, AS自动生成对应的接口class, 如下:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.huawei.hms.ads.identifier.aidl;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Parcel;
import android.os.RemoteException;
public interface OpenDeviceIdentifierService extends IInterface {
String getOaid() throws RemoteException;
boolean isOaidTrackLimited() throws RemoteException;
public abstract static class Stub {
private static final String DESCRIPTOR = "com.uodis.opendevice.aidl.OpenDeviceIdentifierService";
static final int TRANSACTION_getOaid = 1;
static final int TRANSACTION_isOaidTrackLimited = 2;
public Stub() {
}
public static OpenDeviceIdentifierService asInterface(IBinder var0) {
return new OpenDeviceIdentifierService.Stub.Proxy(var0);
}
static class Proxy implements OpenDeviceIdentifierService {
private IBinder mRemote;
Proxy(IBinder var1) {
this.mRemote = var1;
}
public IBinder asBinder() {
return this.mRemote;
}
public String getInterfaceDescriptor() {
return "com.uodis.opendevice.aidl.OpenDeviceIdentifierService";
}
public String getOaid() throws RemoteException {
Parcel var1 = Parcel.obtain();
Parcel var2 = Parcel.obtain();
String var3;
try {
var1.writeInterfaceToken(this.getInterfaceDescriptor());
this.mRemote.transact(1, var1, var2, 0);
var2.readException();
var3 = var2.readString();
} finally {
var2.recycle();
var1.recycle();
}
return var3;
}
public boolean isOaidTrackLimited() throws RemoteException {
Parcel var1 = Parcel.obtain();
Parcel var2 = Parcel.obtain();
boolean var3;
try {
var1.writeInterfaceToken(this.getInterfaceDescriptor());
this.mRemote.transact(2, var1, var2, 0);
var2.readException();
var3 = 0 != var2.readInt();
} finally {
var2.recycle();
var1.recycle();
}
return var3;
}
}
}
}
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.huawei.hms.ads.identifier;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Keep;
import com.huawei.hms.ads.identifier.aidl.OpenDeviceIdentifierService;
import com.huawei.hms.ads.identifier.aidl.OpenDeviceIdentifierService.Stub;
import java.io.IOException;
@Keep
public class AdvertisingIdClient {
public AdvertisingIdClient() {
}
@Keep
public static AdvertisingIdClient.Info getAdvertisingIdInfo(Context var0) throws IOException {
try {
var0.getPackageManager().getPackageInfo("com.huawei.hwid", 0);
} catch (NameNotFoundException var7) {
throw new IOException("Service not found");
}
a var1 = new a();
Intent var2;
(var2 = new Intent("com.uodis.opendevice.OPENIDS_SERVICE")).setPackage("com.huawei.hwid");
String var11;
if (!var0.bindService(var2, var1, 1)) {
var11 = "bind failed";
throw new IOException(var11);
} else {
AdvertisingIdClient.Info var13;
try {
if (var1.a) {
throw new IllegalStateException();
}
var1.a = true;
OpenDeviceIdentifierService var12 = Stub.asInterface((IBinder)var1.b.take());
var13 = new AdvertisingIdClient.Info(var12.getOaid(), var12.isOaidTrackLimited());
} catch (InterruptedException var8) {
var11 = "bind hms service InterruptedException";
throw new IOException(var11);
} catch (RemoteException var9) {
var11 = "bind hms service RemoteException";
throw new IOException(var11);
} finally {
var0.unbindService(var1);
}
return var13;
}
}
public static boolean isAdvertisingIdAvailable(Context var0) {
boolean var1 = true;
try {
PackageManager var5;
(var5 = var0.getPackageManager()).getPackageInfo("com.huawei.hwid", 0);
Intent var2;
(var2 = new Intent("com.uodis.opendevice.OPENIDS_SERVICE")).setPackage("com.huawei.hwid");
if (var5.queryIntentServices(var2, 0).isEmpty()) {
var1 = false;
}
} catch (NameNotFoundException var3) {
var1 = false;
} catch (Exception var4) {
var1 = false;
}
return var1;
}
@Keep
public static final class Info {
private final String advertisingId;
private final boolean limitAdTrackingEnabled;
public Info(String var1, boolean var2) {
this.advertisingId = var1;
this.limitAdTrackingEnabled = var2;
}
@Keep
public final String getId() {
return this.advertisingId;
}
@Keep
public final boolean isLimitAdTrackingEnabled() {
return this.limitAdTrackingEnabled;
}
}
}
- 在需要调用的客户端类中, 自定义一个connection类:
private final class IdentifierServiceConnection implements ServiceConnection {
private IdentifierServiceConnection() {
}
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.i(TAG, "onServiceConnected");
OpenDeviceIdentifierService service = OpenDeviceIdentifierService.Stub.asInterface(iBinder);
if (null != service) {
try {
String oaid = service.getOaid();
boolean isDisable = service.isOaidTrackLimited();
AdvertisingIdClient.Info info = new AdvertisingIdClient.Info(oaid, isDisable);
output.setText(oaid);
} catch (RemoteException e) {
Log.e(TAG, "getChannelInfo Excepition");
}
}
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.i(TAG, "onServiceDisconnected");
}
}
- bind Service方式:
public void bindService() {
IdentifierServiceConnection serviceConnection = new IdentifierServiceConnection();
Intent intent = new Intent("com.uodis.opendevice.OPENIDS_SERVICE");
intent.setPackage("com.huawei.hwid");
bindService(intent, serviceConnection, Service.BIND_AUTO_CREATE);
}