Service
后臺(tái)執(zhí)行耗時(shí)操作融击,例如音樂(lè)筑公。如果使用Activity
來(lái)播放音樂(lè),可能會(huì)在程序切換或者頁(yè)面消失時(shí)停止尊浪,但是使用Service
則不會(huì)匣屡。
與thread的區(qū)別
service:長(zhǎng)時(shí)間執(zhí)行,不與用戶(hù)發(fā)生交互
thread: 需要進(jìn)行交互际长。如某個(gè)Acitvity
的背景音樂(lè)耸采。
使用
startService()
調(diào)用者與被調(diào)用者沒(méi)有聯(lián)系,就算調(diào)用者消失工育,service
也會(huì)繼續(xù)虾宇。
bindService()
調(diào)用者與被調(diào)用綁定關(guān)系,調(diào)用者消失service
就會(huì)停止如绸。
startService
public class ExampleService extends Service {
private static final String TAG = "ExampleService";
// @androidx.annotation.Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG,"creat");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG,"destroy");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG,"start");
return super.onStartCommand(intent, flags, startId);
}
}
private View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.startButton:
Intent startIntent = new Intent(MainActivity.this, ExampleService.class);
startService(startIntent);
break;
case R.id.stopButton:
Intent stopIntent = new Intent(MainActivity.this, ExampleService.class);
stopService(stopIntent);
break;
default:
break;
}
}
}
bindService
public class BinderService extends Service {
private static final String TAG = "BinderService";
private Mybinder binder = new Mybinder();
public void myMethod() {
Log.i(TAG, "my method");
}
public class Mybinder extends Binder {
public BinderService getService(){
return BinderService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
}
private View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.startBinderService:
bindMyService();
break;
case R.id.stopBinderService:
unbindMyService();
break;
default:
break;
}
}
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
BinderService.Mybinder binder = (BinderService.Mybinder)service;
BinderService myService = binder.getService();
myService.myMethod();
isConnected = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
isConnected = false;
}
};
private void bindMyService() {
Intent intent = new Intent(BinderActivity.this, BinderService.class);
bindService(intent, conn , Context.BIND_AUTO_CREATE);
}
private void unbindMyService() {
if (isConnected) {
Log.i(TAG, "unbind method");
unbindService(conn);
}
}
};
線程問(wèn)題
service
會(huì)占用主線程嘱朽,如果進(jìn)行耗時(shí)操作會(huì)卡線程,所以要使用多線程怔接。多線程的使用方法如下搪泳。
private class MyThread extends Thread {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//調(diào)用
new MyThread().start();
IntentService
區(qū)別于service
,IntentService
是異步扼脐,不需要自己創(chuàng)建線程岸军。
public class ExampleIntentService extends IntentService {
/**
* Creates an IntentService. Invoked by your subclass's constructor.
*
* @param name Used to name the worker thread, important only for debugging.
*/
public ExampleIntentService() {
super("ExampleIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}