如何在后台执行Android任务
问题描述:
我正在制作一个Android应用程序,它从文件即手机SD卡中获取数据,并根据时间,应用程序在html中显示数据,即在Web浏览器中。此应用程序连续运行。我有两个任务,第一次是从连续的文件中计算并获取数据,第二次显示的数据也是连续的。我想要在后台执行第一个任务,并在html中连续显示数据。如何在后台执行Android任务
我不知道该怎么办this..also我在安卓..please帮助新me.thank你..
答
您正在寻找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);
您可能是在寻找'AsyncTask' http://stackoverflow.com/questions/9671546/asynctask-android-example或'AsyncTaskLoader' http://stackoverflow.com/a/ 20279805/2413303或者甚至可能是'RxJava',如果你想要高级的话http://stackoverflow.com/questions/23447077/android-rxjava-non-blocking?rq=1但是如果是这样的话你还应该查看IntentService持久的手术。 – EpicPandaForce 2015-02-24 12:14:20