Android .cancel(true)是异步方法吗
我有一个Android .cancel(true)是异步方法吗,android,android-asynctask,Android,Android Asynctask,我有一个AsyncTask,我在onPreExecute中注册接收器,在onPostExecute和onCancelled中取消注册接收器。此AsyncTask从 onOptionsItemSelected(),它在onDestroy()中被取消 当我调用finish()时,随后将调用onDestroy,并且AsyncTask将被取消,但我收到关于存在未注册接收器的logcat错误!! 这意味着从未调用过onCancelled() 我的问题是,.cancel(true)是一种异步方法吗 代码:
AsyncTask
,我在onPreExecute
中注册接收器,在onPostExecute
和onCancelled中取消注册接收器。此AsyncTask
从
onOptionsItemSelected()
,它在onDestroy()
中被取消
当我调用finish()
时,随后将调用onDestroy
,并且AsyncTask
将被取消,但我收到关于存在未注册接收器的logcat错误!!
这意味着从未调用过onCancelled()
我的问题是,.cancel(true)
是一种异步
方法吗
代码:
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)));
}
}
}
私有类ATSPPBond扩展异步任务{
private int mWaitTime=getApplicationContext().getResources().getInteger(R.integer.int\u max\u wait\u time);
private int mSleepTime=getApplicationContext().getResources().getInteger(R.integer.int\u sleep\u time);
私有布尔MBondingWillbegen=false;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
Log.w(TAG,SubTag.msg(“ATSPPBond.onPreExecute”);
如果(!mBCRCtrl.isReg(mBCR\u债券\u状态更改)){
mBCRCtrl.reg(mBCR\u BOND\u STATE\u CHANGED,BluetoothDevice.ACTION\u BOND\u STATE\u CHANGED);
}否则{
Log.d(TAG,SubTag.msg(“ATSPPBond.onPreExecute”、“[mBCR\u BOND\u STATE\u CHANGED]”+getApplicationContext().getResources().getString(R.string.str\u bcr\u reg\u error));
}
}
@凌驾
受保护的Void doInBackground(Void…参数){
Log.w(TAG,SubTag.msg(“ATSPPBond.doInBackground”);
this.mBondingWillBegin=mSPPBTDevice.createBond();
publishProgress(getApplicationContext().getResources().getString(R.string.status_bonding_to_spp_begin));
如果(此.mBondingWillBegin){
而(!isCancelled()&&(mSPPBTDevice.getBondState()!=BluetoothDevice.BOND_BOND)&&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+“秒”);
SystemClock.sleep(this.mSleepTime);
this.mWaitTime-=this.mSleepTime;
}
}否则{
Log.e(TAG,SubTag.msg(“ATSPPBond.doInBackground”,getApplication().getResources().getString(R.string.str\u bondingProcess\u error));
publishProgress(getApplicationContext().getResources().getString(R.string.status_bonding_to_spp_failed));
}
返回null;
}
@凌驾
受保护的void onProgressUpdate(字符串…值){
super.onProgressUpdate(值);
Log.w(TAG,SubTag.msg(“ATSPPBond.onProgressUpdate”);
mtvProcInProgress.setText(值[0]);
}
@凌驾
受保护的void onPostExecute(void避免){
super.onPostExecute(避免);
Log.w(TAG,SubTag.msg(“ATSPPBond.onPostExecute”);
如果(mBCRCtrl.isReg(mBCR债券状态变更)){
mBCRCtrl.unre(mBCR\u BOND\u STATE\u变更);
}否则{
Log.e(TAG,SubTag.bullet(“onDestroy”,“[mBCR_BOND_STATE_CHANGED]”,+getApplicationContext().getResources().getString(R.string.str_bcr_unr_error));
}
..
..
..
}
@凌驾
取消后受保护的作废(作废避免){
超级。一旦取消(避免);
Log.w(TAG,SubTag.msg(“ATSPPBond.onCancelled”);
如果(mBCRCtrl.isReg(mBCR债券状态变更)){
mBCRCtrl.unre(mBCR\u BOND\u STATE\u变更);
}否则{
Log.e(TAG,SubTag.bullet(“onDestroy”,“[mBCR_BOND_STATE_CHANGED]”,+getApplicationContext().getResources().getString(R.string.str_bcr_unr_error));
}
}
}
您能显示日志吗?您如何知道调用了cancel(true)?你自己叫它吗?你能给我看一下航海日志吗?您如何知道调用了cancel(true)?你自己叫它吗?