是.cancel(true)一个异步方法

时间:2016-02-12 12:44:14

标签: android android-asynctask

我有一个AsyncTask,我在onPreExecute注册接收者,在unregisteronPostExecute注册onCancelled。这个AsyncTask是从中执行的 onOptionsItemSelected(),并在onDestroy()中取消。

当我拨打finish()时,随后将调用onDestroy并取消AsyncTask,但我收到有关未注册的接收者的logcat错误! 这意味着onCancelled()从未调用。

我的问题是,.cancel(true)asynchronous方法吗?

private class ATSPPBond extends AsyncTask<Void, String, Void> {

    private int mWaitTime = getApplicationContext().getResources().getInteger(R.integer.int_max_wait_time);
    private int mSleepTime = getApplicationContext().getResources().getInteger(R.integer.int_sleep_time);

    private boolean mBondingWillBegin = false;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Log.w(TAG, SubTag.msg("ATSPPBond.onPreExecute"));

        if (!mBCRCtrl.isReg(mBCR_BOND_STATE_CHANGED)) {
            mBCRCtrl.reg(mBCR_BOND_STATE_CHANGED, BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        } else {
            Log.d(TAG, SubTag.msg("ATSPPBond.onPreExecute", "[mBCR_BOND_STATE_CHANGED] " + getApplicationContext().getResources().getString(R.string.str_bcr_reg_error)));
        }
    }

    @Override
    protected Void doInBackground(Void... params) {
        Log.w(TAG, SubTag.msg("ATSPPBond.doInBackground"));

        this.mBondingWillBegin = mSPPBTDevice.createBond();
        publishProgress(getApplicationContext().getResources().getString(R.string.status_bonding_to_spp_begin));

        if (this.mBondingWillBegin) {

            while (!isCancelled() && (mSPPBTDevice.getBondState() != BluetoothDevice.BOND_BONDED) && this.mWaitTime > 0) {
                Log.d(TAG, SubTag.bullet("ATSPPBond.doInBackground", "wait to bond to SPP device: " + ((getApplicationContext().getResources().getInteger(R.integer.int_max_wait_time) - this.mWaitTime))/1000 + " seconds"));
                SystemClock.sleep(this.mSleepTime);
                this.mWaitTime -= this.mSleepTime;
            }

        } else {
            Log.e(TAG, SubTag.msg("ATSPPBond.doInBackground", getApplication().getResources().getString(R.string.str_bondingProcess_error)));
            publishProgress(getApplicationContext().getResources().getString(R.string.status_bonding_to_spp_failed));
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        Log.w(TAG, SubTag.msg("ATSPPBond.onProgressUpdate"));

        mtvProcInProgress.setText(values[0]);

    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        Log.w(TAG, SubTag.msg("ATSPPBond.onPostExecute"));

        if (mBCRCtrl.isReg(mBCR_BOND_STATE_CHANGED)) {
            mBCRCtrl.unreg(mBCR_BOND_STATE_CHANGED);
        } else {
            Log.e(TAG, SubTag.bullet("onDestroy", "[mBCR_BOND_STATE_CHANGED]" + getApplicationContext().getResources().getString(R.string.str_bcr_unreg_error)));
        }

        ..
        ..
        ..
    }

    @Override
    protected void onCancelled(Void aVoid) {
        super.onCancelled(aVoid);
        Log.w(TAG, SubTag.msg("ATSPPBond.onCancelled"));

        if (mBCRCtrl.isReg(mBCR_BOND_STATE_CHANGED)) {
            mBCRCtrl.unreg(mBCR_BOND_STATE_CHANGED);
        } else {
            Log.e(TAG, SubTag.bullet("onDestroy", "[mBCR_BOND_STATE_CHANGED]" + getApplicationContext().getResources().getString(R.string.str_bcr_unreg_error)));
        }
    }
}

0 个答案:

没有答案