Android重要组件之一 Service 服务讲解学习(一)
? 在Android有異步處理需要重要的Service和Handler組件,今天首先學(xué)習(xí)一下Service組件,
? ? ?其中涉及到Android Service中進(jìn)程間通信和bindService()的方法下一次在講解學(xué)習(xí)
? 1:首先來看看在文檔中對(duì)于Service的概念的解釋:
? ? Service是Android系統(tǒng)中的一種組件,重要性可以堪比Activity,但是也有顯著的區(qū)別,Activity可以和用戶進(jìn)行交互,但是Service只能運(yùn)行在后臺(tái),不能和用戶直接交互;
? ? 當(dāng)我們退出應(yīng)用的時(shí)候,Service還會(huì)在后臺(tái)運(yùn)行,進(jìn)程不會(huì)結(jié)束;特別注意我們需要使用的Service的時(shí)候,我們需要在AndroidManifset.xml文件的service標(biāo)簽進(jìn)行注冊(cè)
? ? 然后我們我們有兩種啟動(dòng)Service的方法包括(①:Context.startService() 和②: Context.bindService()
? ?2:需要使用Service的地方:
? ? 在文檔中舉了下面兩個(gè)例子
? ? ?最讓我們想到的是應(yīng)該是播放器的時(shí)候,我們可能需要邊聽歌邊干些其他的事情,此時(shí)我們會(huì)退出播放器的應(yīng)用,如果不適用Service的話,一退出應(yīng)該應(yīng)用我們就不能聽歌了;還有比如我們需要通過網(wǎng)絡(luò)獲取數(shù)據(jù),由于使用網(wǎng)絡(luò)獲取數(shù)據(jù)的速度比較慢,此時(shí)我們可以使用Service來在后臺(tái)進(jìn)行獲取更新,隔一段時(shí)間把獲取到的數(shù)據(jù)發(fā)回,而不是讓我每次要獲取數(shù)據(jù)都要打開應(yīng)用;
?3:Service與Activity通信:
Service后端的數(shù)據(jù)最終還是要呈現(xiàn)在前端Activity之上的,
在啟動(dòng)Service的時(shí)候,文檔中這樣描述:Note that services, like other application objects, run in the main thread of their hosting process
?主要該Service,和其他應(yīng)用對(duì)象一樣,運(yùn)行其應(yīng)用進(jìn)程主線程上
? ?4:Service 啟動(dòng)方式:
一、context.startService()
二、context.bindService();
使用Service之前必須在AndroidMainfest.xml 中使用<service android:name=".service的類名"/>進(jìn)行注冊(cè)
?5:Service的使用方法(我今天主要學(xué)習(xí)的是startService)
? ?第一步:自己寫一個(gè)繼承Service類的類
? ?第二步:在AndroidMainfest.xml文件中進(jìn)行注冊(cè)
? ?第三步:startService()
? 6:生命周期:
? 首先看下聲明周期的運(yùn)行圖
? ?
?①: context.startService() 啟動(dòng)Service是會(huì)經(jīng)歷一下幾個(gè)方法
啟動(dòng)的時(shí)候:context.startService() ---> onCreate() ---> onStartCommand()
? 銷毀的時(shí)候:ontext.stopService() ? ---> onDestroy();
?如果Service沒運(yùn)行,則此時(shí)會(huì)先調(diào)用onCreate()方法,然后再調(diào)用onStartCommand();
?如果Service已經(jīng)在運(yùn)行,則只調(diào)用onStartCommand(),Service的onStartCommand(),方法可能會(huì)調(diào)用多次
?②:
?啟動(dòng)時(shí)候:context.bindService() ---> onCreate() --->onBind();
?銷毀的時(shí)候:onUnibind() ---> onDestroy()
onBind() 將會(huì)給客戶端返回一個(gè)IBind接口的實(shí)例,此時(shí)客戶端可以去調(diào)用服務(wù)的方法,不過此時(shí)Activity與Service算是綁定在一起了
意思是說如果Activity退出,那么此時(shí)的Service也會(huì)退出
?
接下來是實(shí)例Demo:
??
繼承Service的方法: import android.app.Service; import android.content.Intent; import android.os.IBinder; public class ServiveTest extends Service { @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { System.out.println("onCreat------->"); super.onCreate(); } @Override public void onDestroy() { // TODO Auto-generated method stub System.out.println("onDestroy------->"); super.onDestroy(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("onStartCommand------->"); return super.onStartCommand(intent, flags, startId); } }? ? 今天就初步學(xué)習(xí)了一下Service的使用context.startService()的方法,下一次會(huì)進(jìn)行使用context.bindService()和進(jìn)程間通信的學(xué)習(xí)
轉(zhuǎn)載于:https://blog.51cto.com/2939716/1376506
總結(jié)
以上是生活随笔為你收集整理的Android重要组件之一 Service 服务讲解学习(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQLite命令行程序说明
- 下一篇: 如何制作网线标签和贴标签