Some ways to access ui thread from other thread
1.Activity.runOnUiThread(Runnable)
2.View.post(Runnable)
3.View.postDelayed(Runnable, long)
4.Handler
when start service, we can use handler+message to access UI thread. Create a Handler in UI thread, and register this Handler in service. Send message to Handler in service, so we can access UI thread by Handler.
5.AsyncTask
Make sure that you access the Android UI toolkit only on the UI thread. 另外service也是run在UI thread之中,所以可以access UI.
service life cycle
1. if start service by Context.startService().
onCreate()->onStartCommand()->onDestroy(). 直到呼叫Context.stopService or stopSelf(). 如果多次呼叫startService. 只要呼叫一次stopService, 則所有呼叫的service都會停止。但service所create的thread不會停止,記得要在onDestroy中停止自己create的thread。
2. if start service by Context.bindService()
onCreate()(只有第一次啟動該service時才會呼叫)-> onDestroy()
No comments:
Post a Comment