/** Start of pseudocode **/
1) After a specified delay, the application requests data from a Bluetooth device.
2) The application saves and analyzes the data.
3) IF the analysis results are abnormal, the application sets off an alarm and returns to step 1) with a shortened delay.
ELSE the application returns to step 1) as normal.
/** End of pseudocode **/
I currently have an implementation working that utilizes a Timer and TimerTasks to analyze some data in a .txt file repeatedly after various delays. Later on in the application's development the data will be received from a Bluetooth device (the Android emulator doesn't support Bluetooth).
I can't have the main UI thread busy handling this stuff - I need some form of asynchro开发者_运维技巧nous (multi-threaded) approach (ie. the TimerTask).
I also need this analysis process to continue even if the user switches to another application.
Do I need to use a Service/IntentService to ensure this Bluetooth reception & analysis procedure remains active?
As a beginner Android developer, I would appreciate any advice on the subject. I am currently trying to understand Services and IntentServices.
If you are trying to understand Service and IntentService, I would recommend you to take a look at this post and related posts. It compares various task execution mechanisms in Android and provides a rough guideline on when to use what.
Yes, services are designed exactly for that: asynchronous tasks that can take a long time and need to keep running even when the activity is closed.
I have done similar task in the past with reading bluetooth data using service. You may need to look into using headless service. The service runs even when you go to other apps and when you come back binds with your activity.
精彩评论