Service是Android四大組件之一,也是可執行的程序,有自己的生命週期。創建、配置Service和創建、配置Activity的過程相似。和Activity一樣,都是從Context派生出來的。——《瘋狂android講義(第二版)》
一.Service的第一種啓動方式
採用start的方式開啓服務
使用Service的步驟:
1.定義一個類繼承
Service
2.在Manifest.xml
文件中配置該Service
3.使用Context的startService(Intent)
方法啓動該Service
4.不再使用時,調用stopService(Intent)
方法停止該服務
使用這種start方式啓動的Service的生命週期如下:onCreate()
--->onStartCommand()
(onStart()
方法已過時) ---> onDestory()
說明:如果服務已經開啓,不會重複的執行onCreate()
, 而是會調用
和onStart()onStartCommand()
。
服務停止的時候調用 onDestory()
。服務只會被停止一次。
特點:一旦服務開啓跟調用者(開啓者)就沒有任何關係了。
開啓者退出了,開啓者掛了,服務還在後臺長期的運行。
開啓者不能調用服務裏面的方法。
二.Service的第二種啓動方式
採用bind的方式開啓服務
使用Service的步驟:
1.定義一個類繼承
Service
2.在Manifest.xml
文件中配置該Service
3.使用Context
的bindService(Intent, ServiceConnection, int)
方法啓動該Service
4.不再使用時,調用unbindService(ServiceConnection)
方法停止該服務
使用這種start方式啓動的Service的生命週期如下:onCreate()
--->onBind()
--->onunbind()
--->onDestory()
注意:綁定服務不會調用onstart()
或者onstartcommand()
方法
特點:bind的方式開啓服務,綁定服務,調用者掛了,服務也會跟着掛掉。
綁定者可以調用服務裏面的方法。
到這裏,兩種方式的區別已經明確了。
問題來了。
綁定者如何調用服務裏的方法呢?
首先定義一個Service
的子類。
public class MyService extends Service {
public MyService() {//構造方法
}
@Override
public IBinder onBind(Intent intent) {//onBind方法返回Binder實類,從而調用接口方法
//返回MyBind對象
return new MyBinder();
}
private void methodInMyService() {
Toast.makeText(getApplicationContext(), "服務裏的方法執行了。。。",
Toast.LENGTH_SHORT).show();
}
/**
* 該類用於在onBind方法執行後返回的對象,
* 該對象對外提供了該服務裏的方法
*/
private class MyBinder extends Binder implements IMyBinder {
@Override
public void invokeMethodInMyService() {
methodInMyService();
}
}
}
自定義的MyBinder
接口用於保護服務中不想讓外界訪問的方法。
public interface IMyBinder {
void invokeMethodInMyService();
}
接着在Manifest.xml
文件中配置該Service
<service android:name=".MyService"/>
在Activity中綁定並調用服務裏的方法
簡單佈局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="start"
android:text="開啓服務"
android:textSize="30sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="invoke"
android:text="調用服務的方法"
android:textSize="30sp" />
</LinearLayout>
綁定服務的Activity:
public class MainActivity extends Activity {
private MyConn conn;
private Intent intent;
private IMyBinder myBinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//開啓服務按鈕的點擊事件
public void start(View view) {
intent = new Intent(this, MyService.class);
conn = new MyConn();
//綁定服務,
// 第一個參數是intent對象,表面開啓的服務。
// 第二個參數是綁定服務的監聽器
// 第三個參數一般爲BIND_AUTO_CREATE常量,表示自動創建bind
bindService(intent, conn, BIND_AUTO_CREATE);
}
//調用服務方法按鈕的點擊事件
public void invoke(View view) {
myBinder.invokeMethodInMyService();
}
private class MyConn implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
//iBinder爲服務裏面onBind()方法返回的對象,所以可以強轉爲IMyBinder類型
myBinder = (IMyBinder) iBinder;
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
}
}
綁定本地服務調用方法的步驟:
- 在服務的內部創建一個內部類 提供一個方法,可以間接調用服務的方法
- 實現服務的onbind方法,返回的就是這個內部類
- 在activity 綁定服務。bindService();
- 在服務成功綁定的回調方法onServiceConnected, 會傳遞過來一個 IBinder對象
- 強制類型轉化爲自定義的接口類型,調用接口裏面的方法。
作者:食夢獸
鏈接:https://www.jianshu.com/p/2fb6eb14fdec
來源:簡書