Android Service類與生命周期詳細介紹
Android Service類與生命周期
Service是Android四大組件與Activity最相似的組件,都代表可執(zhí)行的程序,區(qū)別在于Service一直在后臺運行且沒有用戶界面。
1.Service的類圖和生命周期
先來看看Service的類圖:
接下來看看Service的生命周期:
2.開發(fā)Service
(1)開發(fā)Service需要兩步:
第1步:定義子類,繼承Service
第2步:在AndroidManifest.xml文件中配置Service
(2)創(chuàng)建Service
public class MyService extends Service { // 必須實現(xiàn),綁定該Service時被回調(diào) @Override public IBinder onBind(Intent intent) { return null; } // Service被創(chuàng)建時回調(diào) @Override public void onCreate() { super.onCreate(); // 定義相關(guān)業(yè)務(wù)邏輯 System.out.println("Service is Created"); } // Service被啟動時回調(diào) @Override public int onStartCommand(Intent intent, int flags, int startId) { // 定義相關(guān)業(yè)務(wù)邏輯 System.out.println("Service is Started"); return START_STICKY; } // Service被關(guān)閉之前回調(diào) @Override public void onDestroy() { super.onDestroy(); System.out.println("Service is Destroyed"); } }
(3)配置Service
<application ... <!-- 配置一個Service組件 --> <service android:name=".MyService"> <intent-filter> <!-- 為該Service組件的intent-filter配置action --> <action android:name="com.gc.service.MY_SERVICE" /> </intent-filter> </service> </application>
接下來就可以運行Service了。
(4)啟動和停止Service(一般方式)
// 創(chuàng)建啟動Service的Intent final Intent intent = new Intent(); // 為Intent設(shè)置Action屬性 intent.setAction("com.gc.service.MY_SERVICE"); ... // 啟動指定Serivce startService(intent); ... // 停止指定Serivce stopService(intent);
當程序使用startService()、stopService()啟動、關(guān)閉Service時,Service與訪問者之間無法進行通信、數(shù)據(jù)交換,故下面介紹另一種方式啟動和停止Service。
(5)啟動和停止Service(綁定Service并與之通信)
如果Service和訪問者之間需要進行方法調(diào)用或數(shù)據(jù)交換,則應(yīng)該使用bindService()和unbindService()方法啟動、停止Service。
bindService(Intent intent, ServiceConnection conn, int flags),三個參數(shù)如下: intent:指定要啟動的Service conn:用于監(jiān)聽訪問者與Service之間的連接情況,當訪問者與Service之間連接成功時將回調(diào)該ServiceConnection對象的onServiceConnected(ComponentName name, IBinder service)方法;反之回調(diào)該ServiceConnection對象的onServiceDisconnected(ComponentName name)方法(主動調(diào)用unbindService方法斷開連接時則不回調(diào)) flags:指定綁定時是否創(chuàng)建Service,0:不自動創(chuàng)建;BIND_AUTO_CREATE:自動創(chuàng)建 注意:ServiceConnection對象的onServiceConnected方法中有一個IBinder對象,該對象即可實現(xiàn)與綁定Service之間的通信。 在綁定本地Service的情況下,onBind(Intent intent)方法所返回的IBinder對象將會傳給ServiceConnection對象里onServiceConnected(ComponentName name, IBinder service)方法的service參數(shù),這樣訪問者就可以通過該IBinder對象與Service進行通信。
實際開發(fā)通常會采用繼承Binder(IBinder的實現(xiàn)類)的方式實現(xiàn)自己的IBinder對象。
public class MyService extends Service { private int count; // 定義onBinder方法所返回的對象 private MyBinder binder = new MyBinder(); // 通過繼承Binder來實現(xiàn)IBinder類 public class MyBinder extends Binder { public int getCount() { return count; // 獲取Service的運行狀態(tài) } } // 必須實現(xiàn),綁定該Service時被回調(diào) @Override public IBinder onBind(Intent intent) { System.out.println("Service is Binded"); return binder; // 返回IBinder對象 } // Service被創(chuàng)建時回調(diào) @Override public void onCreate() { super.onCreate(); System.out.println("Service is Created"); count = 100; } // Service被斷開連接時回調(diào) @Override public boolean onUnbind(Intent intent) { System.out.println("Service is Unbinded"); return true; } // Service被關(guān)閉之前回調(diào) @Override public void onDestroy() { super.onDestroy(); System.out.println("Service is Destroyed"); } }
接下來定義一個Activity來綁定該Service,并在該Activity中通過MyBinder對象訪問Service的內(nèi)部狀態(tài)。
在該Activity綁定該Service后,該Activity還可以通過MyBinder對象來獲取Service的運行狀態(tài)。對于Service的onBind(Intent intent)方法返回的IBinder對象來說,Service允許客戶端通過該IBinder對象來訪問Service內(nèi)部的數(shù)據(jù),這樣即可實現(xiàn)客戶端與Service之間的通信。
public class MyServiceTest extends Activity { // Service的IBinder對象 MyService.MyBinder binder; // 定義一個ServiceConnection對象 private ServiceConnection conn = new ServiceConnection() { // 當該Activity與Service連接成功時回調(diào) @Override public void onServiceConnected(ComponentName name, IBinder service) { // 獲取Service的onBind方法所返回的MyBinder對象 binder = (MyService.MyBinder) service; } // 當該Activity與Service斷開連接時回調(diào) @Override public void onServiceDisconnected(ComponentName name) { } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ... // 創(chuàng)建啟動Service的Intent final Intent intent = new Intent(); // 為Intent設(shè)置Action屬性 intent.setAction("com.gc.service.MY_SERVICE"); // 綁定指定Serivce bindService(intent, conn, Service.BIND_AUTO_CREATE); ... binder.getCount(); // 獲取Serivce的count值 ... // 解除綁定Serivce unbindService(conn); } }
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- 詳解Android中的Service
- Android IntentService詳解及使用實例
- Android 如何保證service在后臺不被kill
- android使用NotificationListenerService監(jiān)聽通知欄消息
- Android實現(xiàn)微信自動向附近的人打招呼(AccessibilityService)
- Android AccessibilityService實現(xiàn)微信搶紅包插件
- Android Service中使用Toast無法正常顯示問題的解決方法
- Android基于service實現(xiàn)音樂的后臺播放功能示例
- Android Service的啟動過程分析
相關(guān)文章
Android 使用Vitamio打造自己的萬能播放器(1)——準備
本文主要介紹Android Vitamio,在Android開發(fā)視頻播放器的時候,大家經(jīng)常會遇到系統(tǒng)版本和不同的Android手機不同導(dǎo)致開發(fā)的軟件不能完美適用,這里給大家介紹個播放器插件可以適應(yīng)所有Android設(shè)備2016-07-07Android AbsoluteLayout和RelativeLayout布局詳解
本文主要講解Android AbsoluteLayout和RelativeLayout布局,這里整理了相關(guān)資料,并附示例代碼和效果圖,有興趣的小伙伴可以參考下2016-08-08Android編程將Activity背景設(shè)置為墻紙的簡單實現(xiàn)方法
這篇文章主要介紹了Android編程將Activity背景設(shè)置為墻紙的簡單實現(xiàn)方法,涉及Android簡單的屬性設(shè)置及XML配置修改等相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10ActivityManagerService廣播注冊與發(fā)送示例解析
這篇文章主要為大家介紹了ActivityManagerService廣播注冊與發(fā)送示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03