您好,登錄后才能下訂單哦!
【一】使用Service
這節(jié)課程內(nèi)容包括:使用Service、綁定Service、Service的生命周期
Activity能夠呈現(xiàn)用戶界面,與用戶進(jìn)行交互,
而很多時(shí)候,我們程序不需要與用戶交互,只需要一直在后臺(tái)運(yùn)行,
做一些事物處理,例如:Socket長連接、Http網(wǎng)絡(luò)通信、與服務(wù)器保持推送的連接。
如何創(chuàng)建Service?
建一個(gè)繼承于Service的子類,然后在AndroidManifest.xml中配置即可。
public class MyService extends Service{ public IBinder onBind(Intent intent){ throw new UnsupportOperationException("Not yet implemented"); } }
<Service android:name=".MyService" android:enabled="true" android:exported="Service" > </Service>
android:exported —— 是否向外界公開
android:enabled —— 是否啟用
如何啟動(dòng)Service?
新建一個(gè)Button,在MainActivity中給它設(shè)置一個(gè)事件監(jiān)聽器,然后
startService(new Intent(MainActivity.this,MyService.class));
這樣就能啟動(dòng)服務(wù),接下來建一個(gè)停止服務(wù)的Button,寫上
stopService(new Intent(MainActivity.this,MyService.class))
疑問:啟動(dòng)和停止都創(chuàng)建了一個(gè)Intent,這兩個(gè)新創(chuàng)建的Intent實(shí)例,是否操作是同一個(gè)服務(wù)呢?
回答:是同一個(gè)服務(wù)。因?yàn)榉?wù)的實(shí)例,在操作系統(tǒng)上,只可能有那么一個(gè)。
把Intent定義為成員變量,startService和stopService都傳入這同一個(gè)Intent,效果還是一樣的,
因?yàn)镮ntent只是用來配置程序要啟動(dòng)Service的信息的,具體所要操作的Service還是同一個(gè)Service。
在"系統(tǒng)設(shè)置"-"應(yīng)用"-"運(yùn)行中"就可以看我們寫的服務(wù)是否啟動(dòng)。
現(xiàn)在補(bǔ)充一下,讓我們的Service在后臺(tái)不斷執(zhí)行輸出語句,
這需要重寫onStartCommand方法:
public class MyService extends Service{ public IBinder onBind(Intent intent){ throw new UnsupportOperationException("Not yet implemented"); } @Override public int onStartCommand(Intent intent,int flags,int startId){ new Thread(){ public void run(){ super.run(); while(true){ System.out.println("服務(wù)正在運(yùn)行"); } try{ sleep(100); }catch(InterruptedException e){ e.printStackTrace(); } } }.start(); return super.onStartCommand(intent,flag,startId); } }
這樣,如果啟動(dòng)了服務(wù),就算Activity已經(jīng)退出,它仍然會(huì)在后臺(tái)執(zhí)行。
【二】綁定Service
啟動(dòng)Service還能用綁定Service的方式來啟動(dòng),如何綁定?
在MainActivity加兩個(gè)Button,一個(gè)"BindService",一個(gè)"UnBindService"。
它們觸發(fā)的方法分別是bindService()和unbindService()。
bindService()有三個(gè)參數(shù),Intent、服務(wù)的連接(用于監(jiān)聽服務(wù)的狀態(tài),這里傳入)、傳一個(gè)常量Context.BIND_AUTO_CREATE。
第二個(gè)參數(shù)需要MainActivity實(shí)現(xiàn)ServiceConnection接口,需要實(shí)現(xiàn)重寫以下的方法:
@Override public void onServiceConnected(ComponentName name, IBinder service) { Toast.makeText(MainActivity.this, "onServiceConnected", Toast.LENGTH_SHORT).show(); } @Override public void onServiceDisconnected(ComponentName name) { }
onServiceConnected在服務(wù)被綁定成功時(shí)執(zhí)行,
onServiceDisconnected在服務(wù)所在進(jìn)行崩潰或被殺掉時(shí)被執(zhí)行。
unbindService()的參數(shù)就是this。
接下來執(zhí)行程序,會(huì)發(fā)現(xiàn)出錯(cuò):
java.lang.RuntimeException: Unable to bind to service com.linww.demo.learnservice.MyService@23e111e3 with Intent { cmp=com.linww.demo.learnservice/.MyService }: java.lang.UnsupportedOperationException: Not yet implemented
通過查看發(fā)現(xiàn)是onBind這里拋異常,在這方法返回一個(gè)Binder()對(duì)象就OK了。
@Override public IBinder onBind(Intent intent) { // TODO: Return the communication channel to the service. // throw new UnsupportedOperationException("Not yet implemented"); return new Binder(); }
【三】Service的生命周期
服務(wù)生命周期只需要記得有 onCreate()和onDestroy()。
規(guī)律:同時(shí)啟動(dòng)服務(wù)并且綁定服務(wù),必須同時(shí)停止和解除綁定,服務(wù)才能被停止。
如果Activity與某一個(gè)Service綁定,那么退出這個(gè)Activity,Service也會(huì)被取消綁定。
關(guān)于onStartCommand()方法,如果Service第一次啟動(dòng),是onCreate()后執(zhí)行onStartCommand();
而如果Service已經(jīng)被啟動(dòng)過了,那么再去啟動(dòng)它,例如點(diǎn)擊啟動(dòng)按鈕,
則只會(huì)執(zhí)行onStartCommand(),不會(huì)重復(fù)執(zhí)行onCreate()。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。