public class
requestinternetdata
extends
asynctask
/**
* 後台執行的方法,可以執行非ui執行緒,可以執行耗時的方法。在這個方法裡面執行我們的具體任務
* 可以看到這裡傳進來的是多個引數,這裡對應我們在呼叫方法的時候可以傳進來多個引數,後面會看到
*/@override
protected bitmap doinbackground(string... urls)
return
null;
}/**
* 執行在ui執行緒中,在doinbackground()執行完畢後執行
*/@override
protected void onpostexecute(bitmap along)
/*** 在publishprogress()被呼叫以後執行,publishprogress()用於更新進度
* 一般publishprogress()方法執行在doinbackground方法內部,用於實時檢測進度
*/@override
protected void onprogressupdate(integer... values)
}
public class
requestinternetdata
extends
asynctask
/**
* 後台執行的方法,可以執行非ui執行緒,可以執行耗時的方法。在這個方法裡面執行我們的具體任務
* 可以看到這裡傳進來的是多個引數,這裡對應我們在呼叫方法的時候可以傳進來多個引數,後面會看到
*///定義總長度
long all = 0;
@override
protected bitmap doinbackground(string... urls)
log.d(tag, "doinbackground: 請求完畢 請求結果為 " + result.tostring() );
bitmap = bitmapfactory.decodebytearray(result.tostring().getbytes(),0,result.tostring().getbytes().length);
}else
} catch (exception e)
}return bitmap;
}/**
* 執行在ui執行緒中,在doinbackground()執行完畢後執行
*/@override
protected void onpostexecute(bitmap along)
/*** 在publishprogress()被呼叫以後執行,publishprogress()用於更新進度
* 一般publishprogress()方法執行在doinbackground方法內部,用於實時檢測進度
*/@override
protected void onprogressupdate(long... values)
}
@mainthread
public final asynctaskexecute(params... params)
@mainthread
public final asynctaskexecuteonexecutor(executor exec,
params... params)
}mstatus = status.running;
//顯而易見,在任務執行前呼叫這個方法
onpreexecute();
mworker.mparams = params;
//呼叫執行緒池的方法
exec.execute(mfuture);
return
this;
}
private
static
volatile executor sdefaultexecutor = serial_executor;
public
static
final executor serial_executor = new serialexecutor();
private
static
class
serialexecutor
implements
executor finally
}});
if (mactive == null)
}protected
synchronized
void
schedulenext()
}}private
static
class
serialexecutor
implements
executor finally
}});
if (mactive == null)
}protected
synchronized
void
schedulenext()
}}
private
final workerrunnablemworker;
private
final futuretaskmfuture;
mworker = new workerrunnable() catch (throwable tr) finally
return result;
}};mfuture = new futuretask(mworker) catch (interruptedexception e) catch (executionexception e) catch (cancellationexception e)
}};
public
futuretask(callablecallable)
public
void
run() catch (throwable ex)
if (ran)
set(result);
}} finally
}
mworker = new workerrunnable() catch (throwable tr) finally
return result;
}};
mworker = new workerrunnable() catch (throwable tr) finally
return result;
}};
private result postresult(result result)
mhandler = callbacklooper == null || callbacklooper == looper.getmainlooper()
? getmainhandler()
: new handler(callbacklooper);
private
static
class
internalhandler
extends
handler
@suppresswarnings()
@override
public
void
handlemessage(message msg)
}}
private
static
class asynctaskresult
}
private
void
finish(result result) else
mstatus = status.finished;
}
AsyncTask原始碼分析
就從asynctask的構造方法開始分析 creates a new asynchronous task.this constructor must be invoked on the ui thread.public asynctask mfuture new futuretask mworker...
AsyncTask 原始碼分析
asynctask 內部實現原理主要是兩個執行緒池和乙個handler。兩個執行緒池分別為serialexecutor和threadpoolexecutor。其中serialexecutor是維護乙個有序佇列 threadpoolexecutor是執行任務的執行緒池。handler是internal...
原始碼篇 AsyncTask機制
asynctasknew asynctask 執行在子執行緒中,做耗時操作 string doingbackground string s 執行在主線程中,耗時操作完成,更新ui onpostexecute string s execute string asynctask的execute方法pub...