android服务应用场景,Android Service的使用介绍
簡介
Service是Android應用程序中的一個組件,與用戶不進行交互,可以長期的執行在后臺。當新建一個服務的時候需要在AndroidManifest.xml文件中進行聲明。服務可以通過Context.startService()和Context.bindService()來進行啟動。前者就是開啟一個普通的服務后者是將調用者與服務進行綁定,來進行長時間的通信。
服務類似于其他應用程序的對象,運行在主線程中。這就意味著你如果在服務中進行耗時的操作,你需要開啟一個子線程去處理這個操作,不然在服務中超過20秒未響應會發生ANR導致程序崩潰。IntentService的出現就是為了解決在服務中操作耗時任務的。
服務的開啟方式
兩種方式
startService(Intent intent)
bindService(Intent intent,ServiceConnection Connection,int flags);
兩種方式的區別(和生命周期一起分析)
startService()
startService()開啟服務時服務執行的生命周期方法是 onCreate() --> onStartCommand() --> onDestroy()
其中onCreate()會在服務第一次創建的時候調用,當多次調用startService方法的時候,onCreate只會執行一次,而onStartCommand會執行多次,onDestroy方法會在服務銷毀的時候進行調用
服務的銷毀 該方式下啟動的服務可以調用stopService(Intent intent)或者stopSelf()來進行服務的銷毀。
bindService
bindService綁定服務時服務執行的生命周期方法是 onCreate() --> onBind --> onUnbind--> onDestroy
onCreate,onBind 會在bindService第一次調用的時候去執行,如果多次調用bindService,onCreate和onBind也就執行一次,onUnbind和onDestroy會在解綁服務的時候進行調用。
服務的解綁 該方式下綁定的服務需要調用unbindService(ServiceConnection mConnection)方法來進行服務的解綁操作。
混合開啟服務
所謂的混合開啟服務就是即調用了startService又調用了bindService方法來啟動服務。
這里我們分兩種混合開啟方式
startService() --> bindService()
start1.jpg
接著我們配合這種開啟方式,去關閉服務
stopService()-->unbindService()
stop1-1.jpg
觀察生命周期方法的執行發現,先執行stopService()并沒有把服務銷毀,當調用ubindService()的時候將服務解綁了,并且將服務銷毀了。
unbindService()-->stopService()
stop1-2.jpg
接著觀察生命周期方法,先執行unbindService()方法時執行了解綁服務的操作,當調用stopService()才將服務給銷毀了。
bindService() --> startService()
start2.jpg
* 接著我們配合這種開啟方式,去關閉服務
stopService()-->unbindService()
stop2-1.jpg
unbindService()-->stopService()
stop2-2.jpg
服務與Activity之間的通信方式
Binder對象
通過綁定服務的方式開啟服務,在Service中創建一個Binder,然后聲明一個Binder對象,通過onBind()方法將這個Binder對象返回,然后在Activity中實現ServiceConnection接口,實現內部的方法,然后在onServiceConnected方法中將Service中onBind()返回的Binder對象獲取到,然后通過Binder對象去調用Binder類中的方法(一般都是自定義的),這就完成了Service與Activity之間的通信,也成功的實現了Activity調用服務中的方法了。
Intent對象
通過startService方式開啟服務,需要傳遞intent對象到Service的onStartCommand方法中,通過intent對象可以實現Activity到Service之間通信。(就是Activity傳輸數據到Service中)。
廣播(Broadcast)
同樣,在Service中執行一些耗時的操作的時候,然后將執行的結果通知到Activity中,然后更新UI。可以通在子線程中發送廣播。然后在Activity中接收到注冊好的廣播。獲取到結果,結果的獲取是在UI線程中,直接更新UI。
在Activity中聲明一個廣播。
class MyBroadCast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String data = intent.getStringExtra("flag");
Log.d(TAG, "onReceive: "+data);
}
}
在Activity中注冊一個廣播,用來接受相同action發來的廣播,在綁定服務或者開啟服務的時候注冊這個廣播。
public void registerBroadcast(){
MyBroadCast myBroadCast = new MyBroadCast();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.broadcast.mybroadcast");
registerReceiver(myBroadCast,intentFilter);
}
bindService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bindIntent = new Intent(MainActivity.this, ServiceLifeMethod.class);
// Context.BIND_AUTO_CREATE 表示 活動和服務進行綁定后自動創建服務
//使得onCreate方法得到執行,onStartCommand方法不會執行
bindService(bindIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
//注冊服務
registerBroadcast();
}
});
在服務中合適的地方發送廣播,在Activity中聲明好的廣播的onReceive方法中獲取廣播發來的數據。
/**
* 發送廣播
*/
public void sendBroadcast(){
Intent intent = new Intent();
intent.setAction("com.broadcast.mybroadcast");
intent.putExtra("flag","dashingqi");
sendBroadcast(intent);
}
class MyBroadCast extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String data = intent.getStringExtra("flag");
Log.d(TAG, "onReceive: "+data);
}
}
接口回調
當Service執行一些耗時的操作時,執行完畢需要通知Activty來更新數據,可以通過接口回調方式。
寫一個接口類,提供調用的方法。
public interface MyInterface {
void get();
void getMethod();
}
在Service中的自定義Binder類中,提供一個對外獲取Service的方法。
class MyBinder extends Binder {
public ServiceLifeMethod getService(){
return ServiceLifeMethod.this;
}
}
在Service中提供一個對外注冊接口的方法,通過接口傳進來的接口實例,在合適的地方來調用接口的方法。
private MyInterface myInterface;
public void addCallBack(MyInterface myInterface){
this.myInterface = myInterface;
}
@Override
public void onDestroy() {
Log.d(TAG, "onDestroy: ");
super.onDestroy();
myInterface.get();
}
Activity實現接口類,實現接口中的方法,在方法中書寫回調方法的邏輯,在ServiceConnection接口的回調方法onServiceConnected中獲取到Service實例,去調用Service提供的注冊接口的方法。
public class MainActivity extends AppCompatActivity implements MyInterface
@Override
public void get() {
//Service的onDestroy()發放得到執行
Log.d(TAG, "get: onDestroy方法得到執行");
}
@Override
public void getMethod() {
}
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
ServiceLifeMethod.MyBinder myBinder = (ServiceLifeMethod.MyBinder) service;
Log.d(TAG, "onServiceConnected: ");
ServiceLifeMethod myBinderService = myBinder.getService();
myBinderService.addCallBack(MainActivity.this);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "onServiceDisconnected: ");
}
};
通過bindService方法去綁定服務。
bindService.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bindIntent = new Intent(MainActivity.this, ServiceLifeMethod.class);
// Context.BIND_AUTO_CREATE 表示 活動和服務進行綁定后自動創建服務 使得onCreate方法得到執行,onStartCommand方法不會執行
bindService(bindIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
}
});
服務的使用場景
音樂的播放
下載
問題記錄
如何保證Service不被殺死
提高進程的優先級,降低進程被殺死的概率。
啟動前臺服務。
提升服務的優先級,在AndroidManifest.xml文件中服務中設置android:priority="1000"這個屬性為最高的優先級。
監控手機的鎖屏或者解鎖事件,在鎖屏時啟動一個像素的activity,在用戶解鎖時將Activity銷毀。
在進程殺死之后,進行拉活。
注冊高頻率的廣播接收器,比如網絡的變化,解鎖手機屏幕,開機等。
雙進程相互喚起。
依靠系統喚起。
依靠第三方
Android這邊依靠終端的不同,去集成不同終端的推送,小米集成小米推送,華為集成華為推送;
總結
以上是生活随笔為你收集整理的android服务应用场景,Android Service的使用介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux date抖动,一场由fork
- 下一篇: windows文件如何按照创建日期排序按