栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

BlackBerry类是否等效于AsyncTask?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

BlackBerry类是否等效于AsyncTask?

Vishal处在正确的轨道上,但要与Android的版本相匹配还需要更多

AsyncTask
。由于BlackBerry上Java
1.3无法提供枚举和泛型,因此您无法完美匹配Android API。

但是,您可以执行以下操作(未经测试……这只是您的起点):

import net.rim.device.api.ui.UiApplication;public abstract class AsyncTask {    public static final int FINISHED = 0;    public static final int PENDING = 1;    public static final int RUNNING = 2;    private int _status = PENDING;    private boolean _cancelled = false;    private Thread _worker;        public abstract Object doInBackground(Object[] params);    protected void onPreExecute() {        // default implementation does nothing    }    protected void onPostExecute(Object result) {        // default implementation does nothing    }    protected void onProgressUpdate(Object[] values) {        // default implementation does nothing    }    protected void onCancelled() {        // default implementation does nothing    }    protected void onCancelled(Object result) {        onCancelled();    }    public final int getStatus() {        return _status;    }    public final boolean isCancelled() {        return _cancelled;    }    public final boolean cancel(boolean mayInterruptIfRunning) {        if (_status == FINISHED || _cancelled) { return false;        } else { _cancelled = true; if (mayInterruptIfRunning && _status == RUNNING) {     // NOTE: calling Thread.interrupt() usually doesn't work     //   well, unless you don't care what state the background     //   processing is left in.  I'm not 100% sure that this is how     //   Android's AsyncTask implements cancel(true), but I      //   normally just cancel background tasks by letting the     //   doInBackground() method check isCancelled() at multiple     //   points in its processing.     _worker.interrupt(); } return true;        }    }    protected final void publishProgress(final Object[] values) {        // call back onProgressUpdate on the UI thread        UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() {     onProgressUpdate(values); }        });    }    private void completeTask(final Object result) {        // transmit the result back to the UI thread        UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() {     if (isCancelled()) {         onCancelled(result);     } else {         onPostExecute(result);     }     // TODO: not sure if status should be FINISHED before or after onPostExecute()     _status = FINISHED; }        });     }    public AsyncTask execute(final Object[] params) throws IllegalStateException {        if (getStatus() != PENDING) { throw new IllegalStateException("An AsyncTask can only be executed once!");        } else { try {     onPreExecute();     _worker = new Thread(new Runnable() {         public void run() {  try {      // run background work on this worker thread      final Object result = doInBackground(params);      completeTask(result);  } catch (Exception e) {      // I believe if Thread.interrupt() is called, we'll arrive here      completeTask(null);  }         }     });     _status = RUNNING;     _worker.start(); } catch (Exception e) {     // TODO: handle this exception }        }        return this;    }}

另外,请务必牢记适用于上述实现的Android AsyncTask线程规则:

线程规则 为了使此类正常工作,必须遵循一些线程规则:

  • 必须在UI线程上加载AsyncTask类。从JELLY_BEAN开始,这是自动完成的。

  • 必须在UI线程上创建任务实例。

  • 必须在UI线程上调用execute(Params …)。

  • 不要手动调用onPreExecute(),onPostExecute(Result),doInBackground(Params
    …),onProgressUpdate(Progress …)。

  • 该任务只能执行一次(如果尝试第二次执行,则将引发异常。)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/497351.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号