在后台的Andr​​oid不断加速处理后台、Andr、oid

2023-09-06 11:03:08 作者:爱意藏在眼里

我开发需要连续处理加速度值一段时间的应用程序(约一小时) - 是的,我真的想这样做的。在处理涉及做一些计算和更新UI不时。加速度是(前台)服务中收集 - 收集的加速,如果用户最小化应用程序,以及应该发生

我的问题是:什么是执行这个最合适的模式。我不想处理,以使应用程序UI响应。

现在,我收集加速度数据在UI线程(在服务)。所收集的数据比传递到一类不会一些计算和基于它们播放器意图,其被登记在主活动来更新用户界面。

我应该在一个工作线程来收集加速度数据的服务吗? (这甚至有可能)?或者我应该做的另一线程中处理? (如何将这样做没有挨饿无论是UI /加速收集线程或处理线程的最佳方式)。

谢谢!

解决方案

尝试一个AsyncTask的

 公共类AsyncTaskTestActivity延伸活动{   @覆盖   公共无效的onCreate(捆绑savedInstanceState){      super.onCreate(savedInstanceState);      的setContentView(R.layout.main);      新PostTask()执行();   }   //我们的任务类的定义   私有类PostTask扩展的AsyncTask<字符串,整数,字符串> {   @覆盖   在preExecute保护无效(){      super.on preExecute();      displayProgressBar(工作......);   }   @覆盖   保护字符串doInBackground(字符串... PARAMS){         do_background_Stuff();         //每当要在UI线程中运行的东西,然后回来加工调用此方法         publishProgress(ⅰ);      }      返回全部完成!   }   @覆盖   保护无效onProgressUpdate(整数...值){      super.onProgressUpdate(值);      updateProgressBar(值[0]);   }   @覆盖   保护无效onPostExecute(字符串结果){      super.onPostExecute(结果);      dismissProgressBar();   }   }} 

I am developing an app that needs to process acceleration values continuously for some time (approx. an hour) - yes, I would really like to do it that way. The processing involves doing some calculations and updating the UI from time to time. The acceleration is collected inside a (foreground) service - collecting acceleration should happen if the user minimizes the app as well.

My question is: what would be the most appropriate pattern to implement this. I don't want the processing to make the app UI unresponsive.

Right now I'm collecting acceleration data in the UI thread (in the Service). The collected data is than passed to a class that does some calculations and based on them broadcasts an Intent, which is registered in the Main activity to update the UI.

Should I be collecting acceleration data in a worker Thread in a service? (Is this even possible)? Or should I do the processing in another Thread? (How would be the best way to do this without starving either the UI/acceleration collection Thread or the processing Thread).

Thanks!

解决方案

Try an AsyncTask

public class AsyncTaskTestActivity extends Activity {

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      new PostTask().execute();
   }

   // The definition of our task class
   private class PostTask extends AsyncTask<String, Integer, String> {
   @Override
   protected void onPreExecute() {
      super.onPreExecute();
      displayProgressBar("Working...");
   }

   @Override
   protected String doInBackground(String... params) {
         do_background_Stuff();

         // call this method whenever you want to run something in the UI thread and then get back to processing
         publishProgress(i);
      }
      return "All Done!";
   }

   @Override
   protected void onProgressUpdate(Integer... values) {
      super.onProgressUpdate(values);
      updateProgressBar(values[0]);
   }

   @Override
   protected void onPostExecute(String result) {
      super.onPostExecute(result);
      dismissProgressBar();
   }
   }
}