【问题标题】:how to do Android task in background如何在后台执行Android任务
【发布时间】:2015-02-24 12:07:11
【问题描述】:

我正在制作一个从文件(即手机 SD 卡)中获取数据的 Android 应用程序,并根据时间,应用程序以 html 格式(即在网络浏览器中)显示数据。此应用程序连续运行。我有两个任务,第一次计算并从中获取数据文件是连续的,第二个在网络浏览器上显示数据也是连续的。我想在后台运行第一个任务和其他在 html 中连续显示数据的任务。

我不知道该怎么做..我也是android新手..请帮助我。谢谢..

【问题讨论】:

标签: android


【解决方案1】:

您正在寻找AsyncTask。 AysncTask 具有 doInBackground() 方法来完成您的耗时任务。所有其他方法都在主线程上运行。像这样的

new AsyncTask<Void, String, String>() {

            @Override
            protected void onPreExecute() {
                // before executing background task. Like showing a progress dialog
            }

            @Override
            protected String doInBackground(Void... params) {
                // Do background task here
            }

            @Override
            protected void onPostExecute(String msg) {
               // This will be executed when doInBackground() is finished.
               // "msg" is value returned by doInBackground()
            }
        }.execute(null, null, null);

【讨论】:

猜你喜欢
  • 2012-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-28
  • 2013-06-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多