Service服務(wù)
??Android四大金剛之一棺耍,不依賴與UI和組件,可以獨立的運行在后臺。
Service的使用:
一舒帮、startService
- 創(chuàng)建一個類繼承于Service
- 重寫Service里面的onStart或者onStartCommand,已經(jīng)onCreate陡叠、onBind玩郊、onDestory方法
public class MyService extends Service {
public MyService() {
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
/**
* 處理業(yè)務(wù)邏輯的地方
* @param intent
* @param flags
* @param startId
* @return
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
- 使用startService打開服務(wù)
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
- 需要在mainifest中聲明
<service android:name=".MyService"/>
- 關(guān)閉Service
stopService(intent)
Service的優(yōu)先級:Service的優(yōu)先級比不可見的Activity的優(yōu)先級高,比可見的Activity優(yōu)先級底枉阵。
二译红、BindService
??綁定模式下的Service會依賴于組件,非綁定模式下的Service不依賴于組件兴溜。
- 編寫一個類繼承于Service
public class MyBinderStartService extends Service{
Binder binder=new MyBinder();
public int number=0;
public boolean isLooping=true;
- 在Service里面編寫一個類繼承于Binder
- 在Binder類里面編寫一個方法返回當前這個Service的實例:Service.this
public class MyBinder extends Binder{
public MyBinderStartService getService(){
return MyBinderStartService.this;
}
}
- 在Service的onBind方法里面返回自定義的這個類的實例
@Override
public IBinder onBind(Intent intent) {
Log.e("---------------------","onBind");
return binder;
}
- 定義業(yè)務(wù)邏輯的方法
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("---------------------","onStartCommand");
new Thread(new Runnable() {
@Override
public void run() {
while (isLooping){
try {
Thread.sleep(500);
Log.e("----------------------",(number++)+"");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
return 0;
}
- 在需要使用這個Service的組件中來進行調(diào)用
- 首先需要準備Intent
Intent mIntent=new Intent(MainActivity.this, MyBinderStartService.class);
- 編寫一個類實現(xiàn)于ServiceConnection這個類并且實現(xiàn)里面的onServiceConnected和onDisServiceConnected
- 調(diào)用BindService(intent,MyServiceConn,flag)
- 首先需要準備Intent
/**
* 連接那個Service
*/
private class MyServiceConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
MyBinderStartService.MyBinder myBinder=(MyBinderStartService.MyBinder)binder;
myBinderStartService = myBinder.getService();
Log.e("---------------","綁定成功........");
}
/**
* 綁定零食出現(xiàn)問題或者 被系統(tǒng)殺死才會調(diào)用這個方法
* @param name
*/
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e("----------------","我被殺死...............");
}
}
綁定Service
bindService(mIntent,myServiceConn,BIND_AUTO_CREATE);
- 在需要調(diào)用事件的地方調(diào)用Service方法侦厚。
注意:BindService這個方法是異步的,如果在綁定的同時立馬執(zhí)行調(diào)用方法拙徽,不會成功刨沦,BindService的stop方法是沒用的,當解除綁定時會調(diào)用onDestroy方法膘怕,Service就死了想诅。
三、IntentService
??也是一個Service岛心,在IntentService里面封裝了異步任務(wù)(不需要new線程)来破,在運行完所有任務(wù)之后會自動終止Service的運行。
IntentService的使用:
- 自定義一個類繼承于IntentService忘古,并重寫構(gòu)造函數(shù)
public class MyIntentSevice extends IntentService {
public MyIntentSevice() {
super("小子");
}
- 重寫里面的onHandleIntent方法
onHandleIntent:系統(tǒng)new好的異步任務(wù)的回調(diào)徘禁,可以完成耗時操作。
/**
* 必須重寫這個方法:系統(tǒng)給你new好的異步任務(wù)的回調(diào)
* 這個里面可以完成那個耗時的操作
* @param intent
*/
@Override
protected void onHandleIntent(Intent intent) {
try {
Thread.sleep(5000); //用睡眠來模擬那個耗時的操作
Log.e("------------------",Thread.currentThread().getName());
Log.e("---------------","我執(zhí)行完成了.....");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
- 在mainifest中聲明
- 在Activity中打開服務(wù)
Intent intent = new Intent(MainActivity.this, MyBindService.class);
startService(intent);
Notification通知
- 通過NotificationManger獲取管理者對象
NotificationManager mNotificationManager=null;
//第一步:通過NotificationManager獲取那個管理者對象
mNotificationManager= (android.app.NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
- 使用Notification的Builder獲取一個builder對象
Notification.Builder builder = new Notification.Builder(IntentServiceActivity.this);
- 通過Builder對象設(shè)置相應(yīng)的值
//通過builder對象設(shè)置相應(yīng)的值
mBuilder.setContentTitle("天氣預(yù)報"); //設(shè)置標題
mBuilder.setContentText("城市的天氣預(yù)報正在掌握中..."); //設(shè)置那個提示的內(nèi)容
mBuilder.setSmallIcon(R.mipmap.ic_launcher); //設(shè)置前面的那個圖標
mBuilder.setAutoCancel(true); //點擊了那個通知之后那個通知是要消失的
- 顯示通知
Notification mNotification=mBuilder.build();
mNotificationManager.notify(34,mNotification);
- 可以通過id取消單個通知存皂,也可以通過cancelAll取消同一個通知管理者下的所有通知晌坤。
mNotificationManager.cancel(34);
mNotificationManager.cancelAll();