Android AsyncTask的用法
AsyncTask<Params,Progress,Result>
·Params为传入参数类型
·Progress为处理参数类型
·Result为返回类型
例如:一个连接类
private void connect() {
new AsyncTask() {
@Override
protected String doInBackground(Void... params) {
if (mGlobals.mCameraDevice ==null) {
List detectedList = CameraDeviceDetector.detect(DeviceInterface.WLAN);
if (detectedList.isEmpty() ==true) {
return getString(R.string.not_find);
}
mGlobals.mCameraDevice = addEventListenerIfNeed(detectedList.get(0));
}else if (mGlobals.mCameraDevice.isConnected(DeviceInterface.WLAN) ==false) {
List detectedList = CameraDeviceDetector.detect(DeviceInterface.WLAN);
if (detectedList.isEmpty() ==false) {
mGlobals.mCameraDevice = addEventListenerIfNeed(detectedList.get(0));
}
}
Response response =mGlobals.mCameraDevice.connect(DeviceInterface.WLAN);
if (response.getResult() == Result.OK) {
mGlobals.addedEventTextBuffer("connected!");
mListener.showEvent(mGlobals.mEventTextBuilder.toString(),mEventTextView);
retrieveViewInfo();
return getString(R.string.succeed, getString(R.string.connect));
}else {
return getString(R.string.failed, getString(R.string.connect));
}
}
@Override
protected void onPostExecute(String result) {
mStateView.setText(result);
showViewInfo();
}
}.execute();
}