当前位置: 首页 > news >正文

2网站建设搜索引擎推广和优化方案

2网站建设,搜索引擎推广和优化方案,behind设计网站,企业邮箱注册申请官网AsyncTask AsyncTask用于处理耗时任务,可以即时通知进度,最终返回结果。可以用于下载等处理。 使用 实现类继承三个方法 1. doInBackground后台执行,在此方法中进行延时操作 /*** 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结果返回,此结果即为后台执行方法返回的结果。

    /*** <p>Runs on the UI thread after {@link #doInBackground}. The* specified result is the value returned by {@link #doInBackground}.</p>* * <p>This method won't 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 AsyncTask<Params, Progress, Result>

4. execute启动方法

    /*** Executes the task with the specified parameters. The task returns* itself (this) so that the caller can keep a reference to it.* * <p>Note: 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.** <p>This 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 AsyncTask<Params, 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 WorkerRunnable<Params, 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 FutureTask<Result>(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 WorkerRunnable<Params, Result> implements Callable<Result> {Params[] mParams;}

1.3 FutureTask实现对象 mFuture

FutureTask其中保存了Callable接口。

    public FutureTask(Callable<V> var1) {if (var1 == null) {throw new NullPointerException();} else {this.callable = var1;this.state = 0;}}

2. exectue执行操作开始

    @MainThreadpublic final AsyncTask<Params, 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.* * <p>This 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.* * <p><em>Warning:</em> Allowing multiple tasks to run in parallel from* a thread pool is generally <em>not</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}.** <p>This 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 AsyncTask<Params, 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判断:

  1. 正在运行的对象调用此方法会抛出异常,正在运行的设置在此方法中。
  2. 已经完成的对象调用此方法会抛出异常,已经完成的设置在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 ArrayDeque<Runnable> mTasks = new ArrayDeque<Runnable>();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 FutureTask<V> implements RunnableFuture<V>{}
public interface RunnableFuture<V> extends Runnable, Future<V> {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 WorkerRunnable<Params, 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 AsyncTaskResult<Result>(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 AsyncTaskResult<Progress>(this, values)).sendToTarget();}}

4. 如何暂停/取消

直接在doInBackground方法中返回Result即可,Result是泛型,可以自定义各种类型的返回值。

http://www.hkea.cn/news/752038/

相关文章:

  • 广州建设委员会官方网站凡科建站下载
  • 全球做网站的公司排名百度一下你就知道官网
  • 小企业网站价格免费发链接的网站
  • 买了空间和域名 怎么做网站哪家公司网站做得好
  • 网站备案是否关闭衡阳网站建设公司
  • 遂昌建设局网站个人怎么做网站
  • 软件开发和网站建设网络营销的未来6个发展趋势
  • 做网站一年多少钱免费seo网站推广
  • 智通人才网东莞最新招聘信息官网seo是如何做优化的
  • 个人做跨境电商网站百度地图导航手机版免费下载
  • 阿里云注册网站之后怎么做网站百度联盟是什么
  • 动画制作视频河南网站排名优化
  • 网站关键词怎么做排名掌门一对一辅导官网
  • 现在什么网站做推广比较好网页设计需要学什么
  • 个人购物网站 怎么建网络营销包括
  • 有没有做鸭的网站工作室招聘广州网站优化工具
  • 深圳营销外深圳网络营销公司seo和sem的联系
  • 专业的网站制作公司哪家好竞价专员是做什么的
  • 海南省建设厅网站百度seo霸屏软件
  • 淄博张店做网站的公司爱站小工具圣经
  • wordpress w3seo优化自学
  • 临沂手机建站模板微信seo排名优化软件
  • 网站管理员怎么做板块建设艺人百度指数排行榜
  • 如何创建企业网站网络舆情处置的五个步骤
  • 做站长工具网站周口seo公司
  • 泉州自助建站系统地推
  • 美国 做网站免费网站建设哪家好
  • 如何做响应式布局网站seo搜索引擎优化期末及答案
  • 电脑系统优化软件十大排名北京网优化seo公司
  • 宁夏网站建设优化外贸网站优化推广