Service (服務(wù)) 屬于 Android 組件之一, 它可以運行在 App 后臺, 提供和該 App 相關(guān)的事情, 比如監(jiān)聽耳機孔的插拔, 和服務(wù)器保持長連接等. Service 的父類是Activity 父類的父類. 可以將服務(wù)看做是沒有界面的 Activity, Activity 是有界面的組件.
使用服務(wù)需要首先在清單文件中注冊, 它默認(rèn)運行在主線程, 在創(chuàng)建時不可以進行耗時操作. Android 系統(tǒng)規(guī)定, Activity 5s 沒有響應(yīng), Service 10s 沒有響應(yīng), 就會發(fā)生 ANR 錯誤.
服務(wù)和線程的區(qū)別
Android 系統(tǒng)運行時, 會盡量保存每一個運行的 App 進程. 內(nèi)存不夠用時, 會首先回收老進程; 當(dāng)內(nèi)存足夠時, 會率先恢復(fù)帶有服務(wù)的進程. 所以, 將 App 相關(guān)功能放在服務(wù)中, 在一定程度上能夠提高 App 的留存率.
Android 中進程的分類
- Visible Progress 可視進程, 可以看到抢野,但是不能直接交互;
- Service Process 進程中在后臺運行的服務(wù);
- Background Process 不可見葱椭;在后臺運行;
- Empty Process: 空進程狼犯;沒有服務(wù) 也沒有 activity 已經(jīng)銷毀
它們的優(yōu)先級: 可視進程 > 服務(wù)進程 > 后臺進程 > 空進程. 當(dāng)內(nèi)存不足時, 首先回收優(yōu)先級較低的進程;
使用服務(wù)的兩種方式
1 啟動方式
采用啟動模式啟動的服務(wù), 在應(yīng)用退出后, 服務(wù)不會銷毀, 除非用戶通過設(shè)置中的引用管理關(guān)閉服務(wù).
public void startService(View view) {
Intent intent = new Intent();
intent.setClass(this, MyService.class);
startService(intent);
}
public void stopService(View view) {
Intent intent = new Intent();
intent.setClass(this, MyService.class);
stopService(intent);
}
2 綁定方式
采用綁定方式啟動的服務(wù), 隨著應(yīng)用退出而銷毀. 綁定服務(wù)能夠調(diào)用服務(wù)中的私有方法. 使用步驟如下:
定義服務(wù)
public class MyService extends Service {
private class MyAgent extends Binder implements IService{
@Override
public void callMethodInService(String name, int money) {
methodInService(name,money);
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "服務(wù)命令執(zhí)行了", Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}
MyAgent agent;
@Nullable
@Override
public IBinder onBind(Intent intent) {
agent = new MyAgent();
return agent;
}
@Override
public boolean onUnbind(Intent intent) {
Toast.makeText(this, "解綁了服務(wù)", Toast.LENGTH_SHORT).show();
agent = null;
return super.onUnbind(intent);
}
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "服務(wù)創(chuàng)建了", Toast.LENGTH_SHORT).show();
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "服務(wù)銷毀了", Toast.LENGTH_SHORT).show();
}
private void methodInService(String name, int money){
Toast.makeText(this, "服務(wù)方法調(diào)用了", Toast.LENGTH_SHORT).show();
}
}
定義服務(wù)接口
public interface IService {
public void callMethodInService(String name, int money);
}
使用
private MyConn conn;
private IService agent;
private class MyConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
agent = (IService) service;
Toast.makeText(MainActivity.this, "服務(wù)綁定時調(diào)用 onServiceConnected ", Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceDisconnected(ComponentName name) {
agent = null;
Toast.makeText(MainActivity.this, "服務(wù)解綁時調(diào)用 onServiceDisconnected ", Toast.LENGTH_SHORT).show();
}
}
public void bindService(View view) {
Intent intent = new Intent();
intent.setClass(this, MyService.class);
if (conn == null){
conn = new MyConn();
bindService(intent, conn, BIND_AUTO_CREATE);
}
}
public void unBindService(View view) {
if (conn != null){
unbindService(conn);
conn = null;
}
}
public void callMethodInService(View view) {
if (agent != null){
agent.callMethodInService("黑豹", 200);
}else {
Toast.makeText(this, "請先綁定服務(wù)", Toast.LENGTH_SHORT).show();
}
}
混合開啟服務(wù)
既要通過開啟服務(wù), 讓服務(wù)一直運行在后臺; 同時又要調(diào)用服務(wù)中的方法, 稱為混合開啟服務(wù). 其使用要嚴(yán)格遵守以下流程:
- 開啟服務(wù)
- 綁定服務(wù)
- 調(diào)用服務(wù)中的方法
- 解綁服務(wù)
- 關(guān)閉服務(wù)
以上就是服務(wù)的簡單介紹翁逞,更多功能待項目中發(fā)現(xiàn)。