AIDL接口调用
程序员文章站
2022-06-16 09:56:45
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 byt...
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;
}
本文地址:https://blog.csdn.net/weixin_46297800/article/details/114292520