2015-02-24 127 views
-1

我正在製作一個Android應用程序,它從文件即手機SD卡中獲取數據,並根據時間,應用程序在html中顯示數據,即在Web瀏覽器中。此應用程序連續運行。我有兩個任務,第一次是從連續的文件中計算並獲取數據,第二次顯示的數據也是連續的。我想要在後臺執行第一個任務,並在html中連續顯示數據。如何在後臺執行Android任務

我不知道該怎麼辦this..also我在安卓..please幫助新me.thank你..

+0

您可能是在尋找'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

回答

0

您正在尋找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);