Service 需要在manifest.xml中注冊(cè),Service運(yùn)行于主線程,和Thread沒有任何關(guān)系,耗時(shí)操作需要再開線程
startService
start 方式開啟一個(gè)Service
- 創(chuàng)建Service
Service 是一個(gè)抽象類,需要?jiǎng)?chuàng)建一個(gè)子類,并重新相關(guān)生命周期的方法
public class TestService extends Service {
public static final String TAG = "TestService";
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
- 配置Service
<service android:name="com.speed.test.TestService"/>
- 開啟/關(guān)閉Service
Intent serviceIntent = new Intent(this, TestService.class);
startService(serviceIntent);
//stopService(serviceIntent);
調(diào)用了stop后Service會(huì)執(zhí)行onDestory方法
- onStartCommand()的返回值:
在系統(tǒng)因資源不足時(shí)殺死這個(gè)service,之后以何種方式restart與這里的返回值有關(guān)
START_NOT_STICKY:被殺后不會(huì)重啟
START_STICKY:會(huì)重啟,除非系統(tǒng)有pending的intent,否則會(huì)丟給onstartCommand()一個(gè)null的intent。這適合于media player一類的service。
START_REDELIVER_INTENT: 適合于下載等需要立即恢復(fù)的工作。
當(dāng)啟動(dòng)一個(gè)Service的時(shí)候,會(huì)調(diào)用該Service中的onCreate()和onStartCommand()方法,當(dāng)字詞啟動(dòng)同一個(gè)Service是不會(huì)再次調(diào)用onCreate,而是直接調(diào)用onStartCommand
bindService
- Service 中onBind返回一個(gè)Binder實(shí)例
private TestBinder mBinder = new TestBinder();
@Override
public IBinder onBind(Intent intent) { return mBinder ;}
public class TestBinder extends Binder {
public void show(){
Log.d("bind service", "show");
}
}
- Activity中創(chuàng)建ServiceConnection進(jìn)行教會(huì)
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
TestService.TestBinder binder = (TestBinder) service;
binder.show();
}
};
- 綁定/解綁Service
bindService(serviceIntent, connection, BIND_AUTO_CREATE);
//unbindService(connection);
解除綁定后Service會(huì)執(zhí)行onDestoty方法
**如果既 start 又bind 則需要stop unbind 才能銷毀Service **
IntentService
它創(chuàng)建了一個(gè)獨(dú)立的工作線程來處理所有的通過onStartCommand()傳遞給服務(wù)的intents。
創(chuàng)建了一個(gè)工作隊(duì)列,來逐個(gè)發(fā)送intent給onHandleIntent()。
不需要主動(dòng)調(diào)用stopSelft()來結(jié)束服務(wù)。因?yàn)椋谒械膇ntent被處理完后,系統(tǒng)會(huì)自動(dòng)關(guān)閉服務(wù)。
默認(rèn)實(shí)現(xiàn)的onBind()返回null
默認(rèn)實(shí)現(xiàn)的onStartCommand()的目的是將intent插入到工作隊(duì)列中
- 創(chuàng)建IntentService的子類
public class TestIntentService extends IntentService {
public TestIntentService() {
super("TestIntentService");
}
@Override
public void onCreate() {
super.onCreate();
Log.d("TestIntentService", "onCreate");
}
@Override
public void onDestroy() {
Log.i("TestIntentService", "onDestroy");
super.onDestroy();
}
@Override
protected void onHandleIntent(Intent intent) {
getBaiduData();
}
private void getBaiduData() {
try {
URL imageUrl = new URL("https://www.baidu.com/");
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
conn.setConnectTimeout(10000);
conn.setRequestMethod("GET");
if(conn.getResponseCode() == 200) {
InputStream is = conn.getInputStream();
Log.d("TestIntentService", readInputStream(is));
}
} catch (IOException e) {
e.printStackTrace();
}
}
private String readInputStream(InputStream is) {
InputStreamReader isReader = new InputStreamReader(is);
BufferedReader bufferReader = new BufferedReader(isReader);
String inputLine = null;
StringBuffer result = new StringBuffer();
try {
while ((inputLine = bufferReader.readLine()) != null) {
result.append(inputLine);
}
} catch (IOException e) {
e.printStackTrace();
}
return result.toString();
}
- 聲明Service
<service android:name="com.speed.test.TestIntentService"/>
- 啟動(dòng), 無需Stop
Intent intServiceIntent = new Intent(this, TestIntentService.class);
startService(intServiceIntent);
Service 和 Thread 的區(qū)別
二者沒有什么關(guān)系,Service是沒有界面的后臺(tái)服務(wù),不能執(zhí)行耗時(shí)操作,在Activity開啟的子線程并不會(huì)自動(dòng)隨Activity的destroy而關(guān)閉。