企业网站建设与管理试题,请求php网站数据库,网站运营与管理试卷,国外好的做电视包装的网站AsyncTask AsyncTask用于处理耗时任务#xff0c;可以即时通知进度#xff0c;最终返回结果。可以用于下载等处理。 使用
实现类继承三个方法
1. doInBackground后台执行#xff0c;在此方法中进行延时操作 /*** Override this method to perform a computation on a back…AsyncTask AsyncTask用于处理耗时任务可以即时通知进度最终返回结果。可以用于下载等处理。 使用
实现类继承三个方法
1. doInBackground后台执行在此方法中进行延时操作 /*** Override this method to perform a computation on a background thread. The* specified parameters are the parameters passed to {link #execute}* by the caller of this task.** This method can call {link #publishProgress} to publish updates* on the UI thread.** param params The parameters of the task.** return A result, defined by the subclass of this task.** see #onPreExecute()* see #onPostExecute* see #publishProgress*/WorkerThreadprotected abstract Result doInBackground(Params... params);2. onProgressUpdate刷新UI /*** Runs on the UI thread after {link #publishProgress} is invoked.* The specified values are the values passed to {link #publishProgress}.** param values The values indicating progress.** see #publishProgress* see #doInBackground*/SuppressWarnings({UnusedDeclaration})MainThreadprotected void onProgressUpdate(Progress... values){}3. onPostExecute结果返回此结果即为后台执行方法返回的结果。 /*** pRuns on the UI thread after {link #doInBackground}. The* specified result is the value returned by {link #doInBackground}./p* * pThis method wont be invoked if the task was cancelled./p** param result The result of the operation computed by {link #doInBackground}.** see #onPreExecute* see #doInBackground* see #onCancelled(Object) */SuppressWarnings({UnusedDeclaration})MainThreadprotected void onPostExecute(Result result) {}Result,Progress,Params三者是AsyncTask的泛型从代码可以看出Result为返回结果类型Progress是刷新进度的类型Params是处理耗时任务时需要传入的类型。
public abstract class AsyncTaskParams, Progress, Result4. execute启动方法 /*** Executes the task with the specified parameters. The task returns* itself (this) so that the caller can keep a reference to it.* * pNote: this function schedules the task on a queue for a single background* thread or pool of threads depending on the platform version. When first* introduced, AsyncTasks were executed serially on a single background thread.* Starting with {link android.os.Build.VERSION_CODES#DONUT}, this was changed* to a pool of threads allowing multiple tasks to operate in parallel. Starting* {link android.os.Build.VERSION_CODES#HONEYCOMB}, tasks are back to being* executed on a single thread to avoid common application errors caused* by parallel execution. If you truly want parallel execution, you can use* the {link #executeOnExecutor} version of this method* with {link #THREAD_POOL_EXECUTOR}; however, see commentary there for warnings* on its use.** pThis method must be invoked on the UI thread.** param params The parameters of the task.** return This instance of AsyncTask.** throws IllegalStateException If {link #getStatus()} returns either* {link AsyncTask.Status#RUNNING} or {link AsyncTask.Status#FINISHED}.** see #executeOnExecutor(java.util.concurrent.Executor, Object[])* see #execute(Runnable)*/MainThreadpublic final AsyncTaskParams, Progress, Result execute(Params... params) {return executeOnExecutor(sDefaultExecutor, params);}分析
相关知识点
线程池Android Handler
1. 构造器 /*** Creates a new asynchronous task. This constructor must be invoked on the UI thread.*/public AsyncTask() {this((Looper) null);}/*** Creates a new asynchronous task. This constructor must be invoked on the UI thread.** hide*/public AsyncTask(Nullable Handler handler) {this(handler ! null ? handler.getLooper() : null);}/*** Creates a new asynchronous task. This constructor must be invoked on the UI thread.** hide*/public AsyncTask(Nullable Looper callbackLooper) {mHandler callbackLooper null || callbackLooper Looper.getMainLooper()? getMainHandler(): new Handler(callbackLooper);mWorker new WorkerRunnableParams, Result() {public Result call() throws Exception {mTaskInvoked.set(true);Result result null;try {Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);//noinspection uncheckedresult doInBackground(mParams);Binder.flushPendingCommands();} catch (Throwable tr) {mCancelled.set(true);throw tr;} finally {postResult(result);}return result;}};mFuture new FutureTaskResult(mWorker) {Overrideprotected void done() {try {postResultIfNotInvoked(get());} catch (InterruptedException e) {android.util.Log.w(LOG_TAG, e);} catch (ExecutionException e) {throw new RuntimeException(An error occurred while executing doInBackground(),e.getCause());} catch (CancellationException e) {postResultIfNotInvoked(null);}}};}1.1 Handler
使用getMainHandler()返回的Handler。消息处理部分可以看到完成处理与进度更新。 private static Handler getMainHandler() {synchronized (AsyncTask.class) {if (sHandler null) {sHandler new InternalHandler(Looper.getMainLooper());}return sHandler;}}private static class InternalHandler extends Handler {public InternalHandler(Looper looper) {super(looper);}SuppressWarnings({unchecked, RawUseOfParameterizedType})Overridepublic void handleMessage(Message msg) {AsyncTaskResult? result (AsyncTaskResult?) msg.obj;switch (msg.what) {case MESSAGE_POST_RESULT:// There is only one resultresult.mTask.finish(result.mData[0]);break;case MESSAGE_POST_PROGRESS:result.mTask.onProgressUpdate(result.mData);break;}}}private void finish(Result result) {if (isCancelled()) {onCancelled(result);} else {onPostExecute(result);}mStatus Status.FINISHED;}SuppressWarnings({UnusedDeclaration})MainThreadprotected void onProgressUpdate(Progress... values) {}1.2 WorkerRunnable实现对象mWorker
使用Callable接口。 private static abstract class WorkerRunnableParams, Result implements CallableResult {Params[] mParams;}
1.3 FutureTask实现对象 mFuture
FutureTask其中保存了Callable接口。 public FutureTask(CallableV var1) {if (var1 null) {throw new NullPointerException();} else {this.callable var1;this.state 0;}}2. exectue执行操作开始 MainThreadpublic final AsyncTaskParams, Progress, Result execute(Params... params) {return executeOnExecutor(sDefaultExecutor, params);}/*** Executes the task with the specified parameters. The task returns* itself (this) so that the caller can keep a reference to it.* * pThis method is typically used with {link #THREAD_POOL_EXECUTOR} to* allow multiple tasks to run in parallel on a pool of threads managed by* AsyncTask, however you can also use your own {link Executor} for custom* behavior.* * pemWarning:/em Allowing multiple tasks to run in parallel from* a thread pool is generally emnot/em what one wants, because the order* of their operation is not defined. For example, if these tasks are used* to modify any state in common (such as writing a file due to a button click),* there are no guarantees on the order of the modifications.* Without careful work it is possible in rare cases for the newer version* of the data to be over-written by an older one, leading to obscure data* loss and stability issues. Such changes are best* executed in serial; to guarantee such work is serialized regardless of* platform version you can use this function with {link #SERIAL_EXECUTOR}.** pThis method must be invoked on the UI thread.** param exec The executor to use. {link #THREAD_POOL_EXECUTOR} is available as a* convenient process-wide thread pool for tasks that are loosely coupled.* param params The parameters of the task.** return This instance of AsyncTask.** throws IllegalStateException If {link #getStatus()} returns either* {link AsyncTask.Status#RUNNING} or {link AsyncTask.Status#FINISHED}.** see #execute(Object[])*/MainThreadpublic final AsyncTaskParams, Progress, Result executeOnExecutor(Executor exec,Params... params) {if (mStatus ! Status.PENDING) {switch (mStatus) {case RUNNING:throw new IllegalStateException(Cannot execute task: the task is already running.);case FINISHED:throw new IllegalStateException(Cannot execute task: the task has already been executed (a task can be executed only once));}}mStatus Status.RUNNING;onPreExecute();mWorker.mParams params;exec.execute(mFuture);return this;}2.1 进行status判断
正在运行的对象调用此方法会抛出异常正在运行的设置在此方法中。已经完成的对象调用此方法会抛出异常已经完成的设置在InternalHandler中调用。 private void finish(Result result) {if (isCancelled()) {onCancelled(result);} else {onPostExecute(result);}mStatus Status.FINISHED;}2.2 调用onPreExecte()方法
子类实现此方法会在耗时操作前执行一些操作。 /*** Runs on the UI thread before {link #doInBackground}.** see #onPostExecute* see #doInBackground*/MainThreadprotected void onPreExecute() {}2.3 执行exec.execute(mFuture)
2.3.1 exec是什么
通过调用链可看出是sDefaultExecutor。在代码中可以看到是SerialExecutor静态内部类对象。 public static final Executor SERIAL_EXECUTOR new SerialExecutor();private static final int MESSAGE_POST_RESULT 0x1;private static final int MESSAGE_POST_PROGRESS 0x2;private static volatile Executor sDefaultExecutor SERIAL_EXECUTOR;private static class SerialExecutor implements Executor {final ArrayDequeRunnable mTasks new ArrayDequeRunnable();Runnable mActive;public synchronized void execute(final Runnable r) {mTasks.offer(new Runnable() {public void run() {try {r.run();} finally {scheduleNext();}}});if (mActive null) {scheduleNext();}}protected synchronized void scheduleNext() {if ((mActive mTasks.poll()) ! null) {THREAD_POOL_EXECUTOR.execute(mActive);}}}2.3.2 execute(final Runnable r)传是mFuture这里是Runnable
public class FutureTaskV implements RunnableFutureV{}
public interface RunnableFutureV extends Runnable, FutureV {void run();
}2.3.3 调用mFuture的run方法
刚才1.3futuretask实现对象-mfuture可以看出 this.callable var1;即此处调用了mWorker的call()方法 public void run() {if (this.state 0 UNSAFE.compareAndSwapObject(this, runnerOffset, (Object)null, Thread.currentThread())) {boolean var9 false;try {var9 true;Callable var1 this.callable;if (var1 ! null) {if (this.state 0) {Object var2;boolean var3;try {var2 var1.call();var3 true;}....}....}....}....}}2.3.4 mWorker调用doInBackground mWorker new WorkerRunnableParams, Result() {public Result call() throws Exception {mTaskInvoked.set(true);Result result null;try {Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);//noinspection uncheckedresult doInBackground(mParams);Binder.flushPendingCommands();} catch (Throwable tr) {mCancelled.set(true);throw tr;} finally {postResult(result);}return result;}};2.3.5 doInBackground返回result调用postResult(result)
这里发送了结果见1.1处理 private Result postResult(Result result) {SuppressWarnings(unchecked)Message message getHandler().obtainMessage(MESSAGE_POST_RESULT,new AsyncTaskResultResult(this, result));message.sendToTarget();return result;}3. 进度刷新
进度刷新可以调用如下方法。大多在doInBackground中使用。 /*** This method can be invoked from {link #doInBackground} to* publish updates on the UI thread while the background computation is* still running. Each call to this method will trigger the execution of* {link #onProgressUpdate} on the UI thread.** {link #onProgressUpdate} will not be called if the task has been* canceled.** param values The progress values to update the UI with.** see #onProgressUpdate* see #doInBackground*/WorkerThreadprotected final void publishProgress(Progress... values) {if (!isCancelled()) {getHandler().obtainMessage(MESSAGE_POST_PROGRESS,new AsyncTaskResultProgress(this, values)).sendToTarget();}}4. 如何暂停/取消
直接在doInBackground方法中返回Result即可Result是泛型可以自定义各种类型的返回值。 文章转载自: http://www.morning.rhchr.cn.gov.cn.rhchr.cn http://www.morning.ngcth.cn.gov.cn.ngcth.cn http://www.morning.xtxp.cn.gov.cn.xtxp.cn http://www.morning.wdlg.cn.gov.cn.wdlg.cn http://www.morning.nzkkh.cn.gov.cn.nzkkh.cn http://www.morning.xpmwt.cn.gov.cn.xpmwt.cn http://www.morning.fqpyj.cn.gov.cn.fqpyj.cn http://www.morning.btqqh.cn.gov.cn.btqqh.cn http://www.morning.cnqwn.cn.gov.cn.cnqwn.cn http://www.morning.fpkdd.cn.gov.cn.fpkdd.cn http://www.morning.fxzlg.cn.gov.cn.fxzlg.cn http://www.morning.krdxz.cn.gov.cn.krdxz.cn http://www.morning.lizpw.com.gov.cn.lizpw.com http://www.morning.xzjsb.cn.gov.cn.xzjsb.cn http://www.morning.3ox8hs.cn.gov.cn.3ox8hs.cn http://www.morning.ftrpvh.cn.gov.cn.ftrpvh.cn http://www.morning.wqpr.cn.gov.cn.wqpr.cn http://www.morning.ljjph.cn.gov.cn.ljjph.cn http://www.morning.hxftm.cn.gov.cn.hxftm.cn http://www.morning.nhlyl.cn.gov.cn.nhlyl.cn http://www.morning.ptwqf.cn.gov.cn.ptwqf.cn http://www.morning.rdxnt.cn.gov.cn.rdxnt.cn http://www.morning.wrtw.cn.gov.cn.wrtw.cn http://www.morning.yxkyl.cn.gov.cn.yxkyl.cn http://www.morning.sjftk.cn.gov.cn.sjftk.cn http://www.morning.bndkf.cn.gov.cn.bndkf.cn http://www.morning.hjwkq.cn.gov.cn.hjwkq.cn http://www.morning.osshjj.cn.gov.cn.osshjj.cn http://www.morning.fbylq.cn.gov.cn.fbylq.cn http://www.morning.fjzlh.cn.gov.cn.fjzlh.cn http://www.morning.cflxx.cn.gov.cn.cflxx.cn http://www.morning.ynstj.cn.gov.cn.ynstj.cn http://www.morning.pqwrg.cn.gov.cn.pqwrg.cn http://www.morning.pbwcq.cn.gov.cn.pbwcq.cn http://www.morning.rbxsk.cn.gov.cn.rbxsk.cn http://www.morning.bwrbm.cn.gov.cn.bwrbm.cn http://www.morning.ailvturv.com.gov.cn.ailvturv.com http://www.morning.sqfrg.cn.gov.cn.sqfrg.cn http://www.morning.jqpyq.cn.gov.cn.jqpyq.cn http://www.morning.beeice.com.gov.cn.beeice.com http://www.morning.knqzd.cn.gov.cn.knqzd.cn http://www.morning.lddpj.cn.gov.cn.lddpj.cn http://www.morning.lrprj.cn.gov.cn.lrprj.cn http://www.morning.gyylt.cn.gov.cn.gyylt.cn http://www.morning.fqljq.cn.gov.cn.fqljq.cn http://www.morning.hmdyl.cn.gov.cn.hmdyl.cn http://www.morning.bfwk.cn.gov.cn.bfwk.cn http://www.morning.xbkcr.cn.gov.cn.xbkcr.cn http://www.morning.wpxfk.cn.gov.cn.wpxfk.cn http://www.morning.xbbrh.cn.gov.cn.xbbrh.cn http://www.morning.fmkjx.cn.gov.cn.fmkjx.cn http://www.morning.uycvv.cn.gov.cn.uycvv.cn http://www.morning.xhqwm.cn.gov.cn.xhqwm.cn http://www.morning.tmzlt.cn.gov.cn.tmzlt.cn http://www.morning.btpll.cn.gov.cn.btpll.cn http://www.morning.grqlc.cn.gov.cn.grqlc.cn http://www.morning.cwgt.cn.gov.cn.cwgt.cn http://www.morning.yunease.com.gov.cn.yunease.com http://www.morning.dbhnx.cn.gov.cn.dbhnx.cn http://www.morning.bgxgq.cn.gov.cn.bgxgq.cn http://www.morning.zztkt.cn.gov.cn.zztkt.cn http://www.morning.njstzsh.com.gov.cn.njstzsh.com http://www.morning.rtqyy.cn.gov.cn.rtqyy.cn http://www.morning.txhls.cn.gov.cn.txhls.cn http://www.morning.xlyt.cn.gov.cn.xlyt.cn http://www.morning.qckwj.cn.gov.cn.qckwj.cn http://www.morning.drspc.cn.gov.cn.drspc.cn http://www.morning.yksf.cn.gov.cn.yksf.cn http://www.morning.gqjzp.cn.gov.cn.gqjzp.cn http://www.morning.qcfcz.cn.gov.cn.qcfcz.cn http://www.morning.jftl.cn.gov.cn.jftl.cn http://www.morning.xpmhs.cn.gov.cn.xpmhs.cn http://www.morning.wkqrp.cn.gov.cn.wkqrp.cn http://www.morning.xxwhz.cn.gov.cn.xxwhz.cn http://www.morning.yhplt.cn.gov.cn.yhplt.cn http://www.morning.rywn.cn.gov.cn.rywn.cn http://www.morning.xjwtq.cn.gov.cn.xjwtq.cn http://www.morning.skqfx.cn.gov.cn.skqfx.cn http://www.morning.klzdy.cn.gov.cn.klzdy.cn http://www.morning.fnhxp.cn.gov.cn.fnhxp.cn