美文网首页
Android AsyncTask的用法

Android AsyncTask的用法

作者: pokola | 来源:发表于2018-11-09 01:58 被阅读0次

    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();

    }

    相关文章

      网友评论

          本文标题:Android AsyncTask的用法

          本文链接:https://www.haomeiwen.com/subject/kdtnxqtx.html