關鍵字:Android Service 服務的啟動和停止方式
前言:苦逼的野生程序員最近在找工作,跑了各種校園招聘。仍然沒有找到Android方面的工作。哎,說多了都是淚啊,進入正題吧。今天筆試題有問到Android中Service兩種啟動和停止方式有什么不同。所以特地來總結一下Service的知識。
Service是Android四大組件之一,也是可執(zhí)行的程序,有自己的生命周期。創(chuàng)建、配置Service和創(chuàng)建、配置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()
說明:如果服務已經開啟,不會重復的執(zhí)行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的方式開啟服務,綁定服務,調用者掛了,服務也會跟著掛掉。
綁定者可以調用服務里面的方法。
到這里,兩種方式的區(qū)別已經明確了。
問題來了。
綁定者如何調用服務里的方法呢?
首先定義一個Service的子類。
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
//返回MyBind對象
return new MyBinder();
}
private void methodInMyService() {
Toast.makeText(getApplicationContext(), "服務里的方法執(zhí)行了。。。",
Toast.LENGTH_SHORT).show();
}
/**
* 該類用于在onBind方法執(zhí)行后返回的對象,
* 該對象對外提供了該服務里的方法
*/
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();
//綁定服務,
// 第一個參數(shù)是intent對象,表面開啟的服務。
// 第二個參數(shù)是綁定服務的監(jiān)聽器
// 第三個參數(shù)一般為BIND_AUTO_CREATE常量,表示自動創(chuàng)建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) {
}
}
}
綁定本地服務調用方法的步驟:
- 在服務的內部創(chuàng)建一個內部類 提供一個方法,可以間接調用服務的方法
- 實現(xiàn)服務的onbind方法,返回的就是這個內部類
- 在activity 綁定服務。bindService();
- 在服務成功綁定的回調方法onServiceConnected, 會傳遞過來一個 IBinder對象
- 強制類型轉化為自定義的接口類型,調用接口里面的方法。