freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

第7章后臺服務(wù)(編輯修改稿)

2025-08-16 12:32 本頁面
 

【文章內(nèi)容簡介】 ? 1. package 。 2. 3. import 。 4. import 。 5. import 。 6. import 。 7. import 。 8. import 。 9. import 。 10. 本地服務(wù) ? 使用線程 11. public class ThreadRandomServiceDemo extends Activity { 12. 13. private static Handler handler = new Handler()。 14. private static TextView labelView = null。 15. private static double randomDouble 。 16. 17. public static void UpdateGUI(double refreshDouble){ 18. randomDouble = refreshDouble。 19. (RefreshLable)。 20. } 21. 22. private static Runnable RefreshLable = new Runnable(){ 23. @Override 24. public void run() { 25. ((randomDouble))。 26. } 27. }。 28. 本地服務(wù) ? 使用線程 29. @Override 30. public void onCreate(Bundle savedInstanceState) { 31. (savedInstanceState)。 32. setContentView()。 33. labelView = (TextView)findViewById()。 34. Button startButton = (Button)findViewById()。 35. Button stopButton = (Button)findViewById()。 36. final Intent serviceIntent = new Intent(this, )。 37. 38. (new (){ 39. public void onClick(View view){ 40. startService(serviceIntent)。 41. } 42. })。 43. 本地服務(wù) ? 使用線程 44. (new (){ 45. public void onClick(View view){ 46. stopService(serviceIntent)。 47. } 48. })。 49. } 50. } 本地服務(wù) ? 服務(wù)綁定 ? 以綁定方式使用 Service,能夠獲取到 Service對象,不僅能夠正常啟動 Service,而且能夠調(diào)用正在運行中的Service實現(xiàn)的公有方法和屬性 ? 為了使 Service支持綁定,需要在 Service類中重載onBind()方法,并在 onBind()方法中返回 Service對象,示例代碼如下 本地服務(wù) ? 服務(wù)綁定 ? 當(dāng) Service被綁定時,系統(tǒng)會調(diào)用 onBind()函數(shù),通過onBind()函數(shù)的返回值,將 Service對象返回給調(diào)用者 1. public class MathService extends Service{ 2. private final IBinder mBinder = new LocalBinder()。 3. 4. public class LocalBinder extends Binder{ 5. MathService getService() { 6. return 。 7. } 8. } 9. 10. @Override 11. public IBinder onBind(Intent intent) { 12. return mBinder。 13. } 14. } 本地服務(wù) ? 服務(wù)綁定 ? 第 11行代碼中可以看出, onBind()函數(shù)的返回值必須是符合 IBinder接口,因此在代碼的第 2行聲明一個接口變量mBinder, mBinder符合 onBind()函數(shù)返回值的要求,因此將 mBinder傳遞給調(diào)用者 ? IBinder是用于進程內(nèi)部和進程間過程調(diào)用的輕量級接口,定義了與遠(yuǎn)程對象交互的抽象協(xié)議,使用時通過繼承Binder的方法實現(xiàn) ? 第 4行代碼繼承 Binder, LocalBinder是繼承 Binder的一個內(nèi)部類 ? 第 5行代碼實現(xiàn)了 getService()函數(shù),當(dāng)調(diào)用者獲取到mBinder后,通過調(diào)用 getService()即可獲取到 Service的對象 本地服務(wù) ? 服務(wù)綁定 ? 調(diào)用者通過 bindService()函數(shù)綁定服務(wù) ? 并在第 1個參數(shù)中將 Intent傳遞給 bindService()函數(shù),聲明需要啟動的 Service ? 第 3個參數(shù) 存在,就自動建立 Service;同時也告知 Android系統(tǒng),這個 Service的重要程度與調(diào)用者相同,除非考慮終止調(diào)用者,否則不要關(guān)閉這個 Service 1. final Intent serviceIntent = new Intent(this,)。 2. bindService(serviceIntent,mConnection,)。 本地服務(wù) ? 服務(wù)綁定 ? bindService()函數(shù)的第 2個參數(shù)是 ServiceConnnection ? 當(dāng)綁定成功后,系統(tǒng)將調(diào)用 ServiceConnnection的onServiceConnected()方法 ? 而當(dāng)綁定意外斷開后,系統(tǒng)將調(diào)用 ServiceConnnection中的 onServiceDisconnected方法 ? 由上可知, 以綁定方式使用 Service,調(diào)用者需要聲明一個 ServiceConnnection,并重載內(nèi)部的onServiceConnected()方法和 onServiceDisconnected方法 本地服務(wù) ? 服務(wù)綁定 ? 在第 4行代碼中,綁定成功后通過 getService()獲取Service對象,這樣便可以調(diào)用 Service中的方法和屬性 ? 第 8行代碼將 Service對象設(shè)置為 null,表示綁定意外失效, Service實例不再可用 1. private ServiceConnection mConnection = new ServiceConnection() { 2. @Override 3. public void onServiceConnected(ComponentName name, IBinder service) { 4. mathService = (()service).getService()。 5. } 6. @Override 7. public void onServiceDisconnected(ComponentName name) { 8. mathService = null。 9. } 10. }。 本地服務(wù) ? 服務(wù)綁定 ? 取消綁定僅需要使用 unbindService()方法,并將ServiceConnnection傳遞給 unbindService()方法 ? 需 注意的是, unbindService()方法成功后,系統(tǒng)并不會調(diào)用 onServiceConnected(),因為 onServiceConnected()僅在意外斷開綁定時才被調(diào)用 unbindService(mConnection)。 本地服務(wù) ? 服務(wù)綁定 ? 通過 bindService()函數(shù)綁定 Servcie時, onCreate()函數(shù)和 onBinde()函數(shù)將先后被調(diào)用 ? 通過 unbindService()函數(shù)取消綁定 Servcie時,onUnbind()函數(shù)將被調(diào)用,如果 onUnbind()函數(shù)的返回true,則表示在調(diào)用者綁定新服務(wù)時, onRebind()函數(shù)將被調(diào)用 ? 綁定方式的函數(shù)調(diào)用順序 調(diào) 用b i n d S e r v i c e ( )啟 動 S e r v i c eo n C r e a t e ( ) o n B i n d ( )o n R e b i n d ( )o n U n b i n d ( ) o n D e s t o r y ( ) 停 止 S e r v i c e客 戶 端 與S e r v i c e 交 互 本地服務(wù) ? 服務(wù)綁定 ? 示例 SimpleMathServiceDemo使用綁定方式使用Service ? 創(chuàng)建了 MathService服務(wù),用來完成簡單的數(shù)學(xué)運算但足以說明如何使用綁定方式調(diào)用 Service實例中的公有方法 本地服務(wù) ? 服務(wù)綁定 ? 在服務(wù)綁定后,用戶可以點擊“加法運算”,將兩個隨機產(chǎn)生的數(shù)值傳遞給 MathService服務(wù),并從 MathService對象中獲取到加法運算的結(jié)果,然后顯示在屏幕的上方 ? “取消綁定”按鈕可以解除與 MathService的綁定關(guān)系,取消綁定后,無法通過“加法運算”按鈕獲取加法運算結(jié)果 本地服務(wù) ? 服務(wù)綁定 ? 在 SimpleMathServiceDemo示例中, 文件是描述 Service的文件 ? 1. package 。 2. 3. import 。 4. import 。 5. import 。 6. import 。 7. import 。 8. 9. public class MathService extends Service{ 10. 本地服務(wù) ? 服務(wù)綁定 11. private final IBinder mBinder = new LocalBinder()。 12. 13. public class LocalBinder extends Binder{ 14. MathService getService() { 15. return 。 16. } 17. } 18. 19. @Override 20. public IBinder onBind(Intent intent) { 21. (this, 本地綁定: MathService, 22. ).show()。 23. return mBinder。 24. } 25. 本地服務(wù) ? 服務(wù)綁定 26. @Override 27. public boolean onUnbind(Intent intent){ 28. (this, 取消本地綁定: MathService, 29. ).show()。 30. return false。 31. } 32. 33. 34. public long Add(long a, long b){ 35. return a+b。 36. } 37. 38. } 本地服務(wù) ? 服務(wù)綁定 ? Activity文件,綁定和取消綁定服務(wù)的代碼在這個文件中 ? 1. package 。 2. 3. import 。 4. import 。 5. import 。 6. import 。 7. import 。 8. import 。 9. import 。 10. import 。 11. import 。 12. import 。 13. 本地服務(wù) ? 服務(wù)綁定 14. public class SimpleMathServiceDemo extends Activity { 15. private MathService mathService
點擊復(fù)制文檔內(nèi)容
黨政相關(guān)相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖片鄂ICP備17016276號-1