AIDL接口调用

AES 加密的过程及代码 封装类





public class ConfigDataManager {
    private String TAG = "ConfigDataManager";
    private Context mContext;
    private byte[] mXmlData;
    private boolean oprSucceed = false;
    private static final int SECOND = 100;
    private static final int REQUEST_COUNT = 30;
public byte[] getmXmlData() {
    if (iConfigService == null) {
        return null;
    }
    try {
        oprSucceed = false;
        mXmlData = null;
        // 调用Aidl 解密每次调用方法都获取一遍数据
        iConfigService.getConfigInfo(configServiceCallback);
    } catch (RemoteException e) {
        Log.e(TAG, "RemoteException");
        return null;
    }
    // 耗时操作等待知道将数据获取完毕
    int count = 0;
    while (!oprSucceed) {
        try {
            Thread.sleep(SECOND);
        } catch (InterruptedException e) {
            Log.e(TAG, "InterruptedException");
            break;
        }
        if (count == REQUEST_COUNT) {
            break;
        }
        count++;
    }
    return mXmlData;
}

public ConfigDataManager(Context context) {
    mContext = context;
    bindService();
}

private IConfigService iConfigService;
private ServiceConnection connection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        iConfigService = IConfigService.Stub.asInterface(service);
        LogHelper.i(TAG, "连接成功");
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        iConfigService = null;
    }
};

// 绑定
public boolean bindService() {
    Intent bindServiceIntent = new Intent(PACKAGE);
    bindServiceIntent.setClassName(PACKAGE_NAME, CLASS_NAME);
    boolean result = mContext.bindService(bindServiceIntent, connection, Context.BIND_AUTO_CREATE);
    Log.i(TAG, "bindService: " + result);
    return result;
}
// aidl 接口回调
private IConfigServiceCallback configServiceCallback = new IConfigServiceCallback.Stub() {
    @Override
    public void onResult(boolean isSucceed, byte[] xmlData) throws RemoteException {
        if (isSucceed) {
            LogHelper.i(TAG, isSucceed + "");
            mXmlData = xmlData;
        } else {
            mXmlData = null;
        }
        oprSucceed = true;
        if (mXmlData != null) {
            LogHelper.i(TAG, isSucceed + "长度 " + mXmlData.length);
        }
    }
};

// 调用
public class AcceptMessages {
private static AcceptMessages acceptMessages;
private static String TAG = “AcceptMessages”;
private final ConfigDataManager iConfig;
private static final int SECOND = 100;
private static final int REQUEST_COUNT = 30;

// 绑定服务 通过构造绑定服务
private AcceptMessages(Context context) {
    iConfig = new ConfigDataManager(context);
}

public static AcceptMessages getInstance(Context context) {
    if (acceptMessages == null) {
        acceptMessages = new AcceptMessages(context);
    }
    return acceptMessages;
}

// 获取数据
public byte[] getIConfigData() {
    byte[] bytes;
    int count = 0;
    // 获取配置文件的数据
    while ((bytes = iConfig.getmXmlData()) == null) {
        try {
            Thread.sleep(SECOND);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (count == REQUEST_COUNT) {
            break;
        }
        count++;
    }
    return bytes;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值