- Service的兩種啟動模式
- startService()
Intent intent = new Intent(this, MyService.class);
startService(intent);
- bindService
Intent intent = new Intent(this, MyService.class);
startService(intent);
ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
Log.d("TAG", "onServiceConnected");
MyService.MyBinder myBinder = (MyService.MyBinder) iBinder;
service = myBinder.getService();
isBind = true;
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
Log.d("TAG", "onServiceDisconnected");
}
};
bindService(intent, conn, Service.BIND_AUTO_CREATE);
- 自定義Service
public class MyService extends Service {
public MyService() {
}
@Override
public void onCreate() {
super.onCreate();
Log.d("TAG", "MyService---->onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("TAG", "MyService---->onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
Log.d("TAG", "MyService---->onBind");
return new MyBinder();
}
@Override
public boolean onUnbind(Intent intent) {
Log.d("TAG","MyService---->onUnbind");
return super.onUnbind(intent);
}
public class MyBinder extends Binder {
public MyService getService() {
return MyService.this;
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("TAG", "MyService---->onDestroy");
}
}
既startService又bindService
從中可以看出onCreate只執(zhí)行一次,onCreate不管startService或者bindService多少次都只執(zhí)行一次onCreate()
- startService和bindService的區(qū)別
startService沒有和Activity綁定,Activity銷毀了之后service還是能夠運(yùn)行铁孵;bindService啟動時(shí)Service和Activity進(jìn)行了綁定杖小,如果Activity銷毀了撵幽,那么Service會自動進(jìn)行解綁和調(diào)用Service的onDestroy()方法 - IntentService
- IntentService的定義
package com.example.ulabor.testproject;
import android.app.IntentService;
import android.content.Intent;
import android.content.Context;
import android.util.Log;
import java.util.concurrent.Executors;
/**
* An {@link IntentService} subclass for handling asynchronous task requests in
* a service on a separate handler thread.
* <p>
* helper methods.
*/
public class MyIntentService extends IntentService {
private static final String ACTION_FOO = "com.example.ulabor.testproject.action.FOO";
private static final String ACTION_BAZ = "com.example.ulabor.testproject.action.BAZ";
private static final String EXTRA_PARAM1 = "com.example.ulabor.testproject.extra.PARAM1";
private static final String EXTRA_PARAM2 = "com.example.ulabor.testproject.extra.PARAM2";
public MyIntentService() {
super("MyIntentService");
}
/**
* Starts this service to perform action Foo with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
public static void startActionFoo(Context context, String param1, String param2) {
Intent intent = new Intent(context, MyIntentService.class);
intent.setAction(ACTION_FOO);
intent.putExtra(EXTRA_PARAM1, param1);
intent.putExtra(EXTRA_PARAM2, param2);
context.startService(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("TAG", "onDestroy");
}
/**
* Starts this service to perform action Baz with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
public static void startActionBaz(Context context, String param1, String param2) {
Intent intent = new Intent(context, MyIntentService.class);
intent.setAction(ACTION_BAZ);
intent.putExtra(EXTRA_PARAM1, param1);
intent.putExtra(EXTRA_PARAM2, param2);
context.startService(intent);
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
final String action = intent.getAction();
if (ACTION_FOO.equals(action)) {
final String param1 = intent.getStringExtra(EXTRA_PARAM1);
final String param2 = intent.getStringExtra(EXTRA_PARAM2);
handleActionFoo(param1, param2);
} else if (ACTION_BAZ.equals(action)) {
final String param1 = intent.getStringExtra(EXTRA_PARAM1);
final String param2 = intent.getStringExtra(EXTRA_PARAM2);
handleActionBaz(param1, param2);
}
}
}
/**
* Handle action Foo in the provided background thread with the provided
* parameters.
*/
private void handleActionFoo(String param1, String param2) {
try {
try {
for (int i = 0; i < 10; i++) {
Thread.sleep(1000);
Log.d("TAG", "i---》" + i);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Handle action Baz in the provided background thread with the provided
* parameters.
*/
private void handleActionBaz(String param1, String param2) {
// TODO: Handle action Baz
throw new UnsupportedOperationException("Not yet implemented");
}
}
最主要的就是onHandleIntent()方法對傳入的參數(shù)進(jìn)行處理象浑;從中可以看出在handleActionFoo方法中榴捡,調(diào)用 Thread.sleep(1000);來進(jìn)行耗時(shí)操作模擬克懊,Service自己循環(huán)了10次之后自動銷毀秸妥。能夠在onHandleIntent()方法中進(jìn)行耗時(shí)操作统捶。因?yàn)镾ervice的onCreate()和onStartCommand方法是在主線程運(yùn)行的榆芦,所以不能夠在這里進(jìn)行耗時(shí)操作。IntentService是竄行操作喘鸟,不是并行操作匆绣,也就是任務(wù)是一步一步完成的,如下圖
執(zhí)行如下代碼:
for (int j = 0; j < 2; j++) {
MyIntentService.startActionFoo(this, "a", "b");
}
運(yùn)行結(jié)果
image.png