Android——Service

一.What is a Service?

服務(wù)是Android中實現(xiàn)后臺運行的解決方案,適合去處理那些不需要與用戶進(jìn)行交互且要求長期去運行的任務(wù)(服務(wù)的運行不依賴于任何的界面,即使當(dāng)程序被切換到后臺的時候,或者用戶打開另一個應(yīng)用程序,服務(wù)仍能夠保持正常的運行)。
注意點:

  • 服務(wù)并不是一個獨立的進(jìn)程,而是運行在創(chuàng)建服務(wù)所在的應(yīng)用程序進(jìn)程當(dāng)中,它只是其中的一部分。
  • 服務(wù)并不是一個線程,它并不是結(jié)束主線程的手段,服務(wù)不會自動的開啟線程,所有的代碼都
    是默認(rèn)在主線程 中的,我們需要在服務(wù)的內(nèi)部創(chuàng)建子線程,并執(zhí)行具體的任務(wù)。
    服務(wù)主要的兩個特征:
  • 執(zhí)行一個應(yīng)用程序告訴系統(tǒng)其想在后臺進(jìn)行的操作,直到明確終止服務(wù) 調(diào)用Context.startService
  • 為其他的應(yīng)用程序提供功能,調(diào)用Context.bindService

二.定義一個服務(wù)

  • onBind()方法是Service中唯一的抽象方法,必須在子類中實現(xiàn)。其他的三個方法:
  • onCreate():創(chuàng)建服務(wù)時調(diào)用,只會在Service第一次被創(chuàng)建的時候調(diào)用,如果當(dāng)前Service已經(jīng)被創(chuàng)建過了,不管怎樣調(diào)用startService()方法,onCreate()方法都不會再執(zhí)行
  • onStartCommand():服務(wù)啟動的時候調(diào)用,希望服務(wù)執(zhí)行的動作,可以寫在此方法中。(注意與onCreate方法區(qū)別,onStartCommand每次啟動服務(wù)都會調(diào)用)
  • onDestroy():服務(wù)銷毀的時候調(diào)用,回收資源的操作可以寫在此方法中。
public class MyService extends Service
 {@Nullable @Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}@Override
public void onDestroy() {super.onDestroy();}
}

服務(wù)需要在AndroidManifest.xml中注冊:

<service android:name=".MyService"></service>

三.啟動停止服務(wù)

@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.start_service:
Intent startIntent=new Intent(this,MyService.class);
startService(startIntent); 
break;
 case R.id.stop_service:
Intent stopIntent=new Intent(this,MyService.class);
stopService(stopIntent); break;
}
}

分別創(chuàng)建兩個Intent對象,調(diào)用startService方法和stopService方法啟動和終止服務(wù)。此外,在服務(wù)中可以通過stopSelf()來終止自己。

四.Service與Activity通信

在Activity中可以指定讓Service去執(zhí)行什么任務(wù),只需要讓Activity和Service建立關(guān)聯(lián)就好了。onBind方法用于與Activity建立關(guān)聯(lián):

private DownloadBinder mBinder=new DownloadBinder();
@Override
public IBinder onBind(Intent intent) {return mBinder;}
private DownloadBinder mBinder=new DownloadBinder();
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private MyService.DownloadBinder downloadBinder;
private ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
downloadBinder(MyService.DownloadBinder)service;downloadBinder.startDownLoad();
}
@Override
public void onServiceDisconnected(ComponentName name) {
};
case R.id.bind_service:
Intent bindIntent=new Intent(this,MyService.class);
bindService(bindIntent,connection,BIND_AUTO_CREATE);
 break;
case R.id.unbind_service:
Intent unbindIntent=new Intent(this,MyService.class);
unbindService(connection);
 break;

創(chuàng)建ServiceConnection匿名類,重寫onServiceConnected,onServiceDisConnected方法,這兩個方法分別會在活動與服務(wù)綁定和解除綁定時調(diào)用。

服務(wù)不僅可以和MainActivity綁定還能與任意一個活動綁定。

五.Service的生命周期

每個服務(wù)都只會存在一個實例,不管調(diào)用多少次startService,只需要調(diào)用一次stopService或stopSelf方法。
活動與服務(wù)創(chuàng)建連接時,(bindService)調(diào)用onBind方法,如果服務(wù)沒有創(chuàng)建過會先調(diào)onCreate方法,然后調(diào)用onBind方法。
startService——stopService
bindService——unbindService 兩種情況onDestroy都會調(diào)用
但是startService,bindService兩種方法都執(zhí)行,必須調(diào)用stopService unbindService 兩個方法都調(diào)用才會執(zhí)行onDestroy
一個服務(wù)只要啟動或者綁定后會一直運行,只有兩個條件同時不滿足,服務(wù)才會銷毀。

六.使用前臺服務(wù)

如果你希望服務(wù)可以一直保持運行狀態(tài),而不會由于內(nèi)存不足的原因?qū)е卤换厥眨涂梢允褂们芭_服務(wù)。

七.使用IntentService

服務(wù)中的代碼是運行在主線程中的,所以在服務(wù)中處理一些耗時的操作,很容易出現(xiàn)ANR(Application not Responding)的情況。

此時可以在Service中開啟子線程處理耗時的操作,但是注意要stopService/stopSelf讓服務(wù)停下來

除此之外,可以采用IntentService:

IntentService中的onHandleIntent方法是在子線程中運行的,且服務(wù)在運行結(jié)束后會自動停止,因此可以在onHandleIntent處理耗時的邏輯。

case R.id.start_intentservice:
Intent intentService=new Intent(this,MyIntentService.class);
startService(intentService);
 break;
public class MyIntentService extends IntentService {
 public MyIntentService(String name) {super(name);}
 @Override
 protected void onHandleIntent(Intent intent) {
 }
 @Override
 public void onDestroy() {
  super.onDestroy();
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容