服務
服務作為Android的四大組件之一,它不像活動那么清晰可見,總是在后台默默的付出。下面讓我們淺顯易懂的學習以下服務。
1.啟動形式
1.直接啟動
通過startService啟動服務,這種啟動形式比較簡單;
- 創建服務
public class MyService extends Service {
public static final String TAG= "MyService";
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG,"onBind(Intent intent)");
return null;
}
@Override
public void onCreate() {
Log.d(TAG,"onCreate()");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG,"onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.d(TAG,"onDestroy()");
super.onDestroy();
}
}
- 創建布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/start"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="開啟事務"/>
<Button
android:id="@+id/stop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="停止服務" />
</LinearLayout>
</LinearLayout>
- 啟動服務
public class MyService extends Service {
public static final String TAG= "MyService";
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG,"onBind(Intent intent)");
return null;
}
@Override
public void onCreate() {
Log.d(TAG,"onCreate()");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG,"onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.d(TAG,"onDestroy()");
super.onDestroy();
}
}
- 在AndroidManfest.xml中聲明
<service android:name=".MyService"/>
注意:1.服務需要繼承Service,onbind()必須被重寫;
2.服務需要在配置文件中配置;
2.綁定啟動
- 創建服務
package com.example.myservice;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
public class MyService extends Service {
public static final String TAG= "MyService";
private Mybind mybind = new Mybind();
class Mybind extends Binder{
public void start(){
Log.d(TAG,"Mybind.start()");
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG,"onBind(Intent intent)");
return mybind;
}
@Override
public void onCreate() {
Log.d(TAG,"onCreate()");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG,"onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.d(TAG,"onDestroy()");
super.onDestroy();
}
}
可以看到onBind()返回了一個binder的對象,服務進行的操作,其實就是這個返回對象實現類的操作。一般都服務的功能主要是下載等功能,這里主要對服務的啟動形式進行分析,功能方面就不說了。
- 布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/start"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="開啟事務"/>
<Button
android:id="@+id/stop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="停止服務" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/bind"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="綁定事務"/>
<Button
android:id="@+id/unbind"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="解除綁定" />
</LinearLayout>
</LinearLayout>
- 啟動服務
package com.example.myservice;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private MyService.Mybind mybind;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myService();
}
private ServiceConnection sc = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
mybind = (MyService.Mybind) iBinder;
mybind.start();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
}
};
private void myService() {
Intent intent = new Intent(this,MyService.class);
Button start = findViewById(R.id.start);
Button stop = findViewById(R.id.stop);
Button bind = findViewById(R.id.bind);
Button unbind = findViewById(R.id.unbind);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startService(intent);
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
stopService(intent);
}
});
bind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bindService(intent,sc,BIND_AUTO_CREATE);
}
});
unbind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
unbindService(sc);
}
});
}
}
可以看到這種啟動形式比上面一種麻煩很多,首先對於服務需要在onbind()中返回一個binder對象。其次bindService(Intent,sc,1)綁定的時候需要傳遞三個參數,第一個是intent,第二個是獲取binder實例的ServiceerConnection對象,通過重寫onServiceConnected(ComponentName componentName, IBinder iBinder)方法獲取binder中要實現的功能,第三個傳遞的是一個字符串,BIND_AUTO_CREATE代表綁定后創建服務;
- 在AndroidManfest.xml中聲明
<service android:name=".MyService"/>
2.生命周期
1.startService生命周期
onCreate() → onStartCommand() → onDestory()
onCreate()服務第一創建的時候調用
onStartCommand()每次啟動服務的時候調用
onDestory()服務銷毀之前調用
2.bindService生命周期
onCreate() →onBind()→ onunbind()→ onDestory()
此時服務生命周期有兩種情況:
1.服務通過startService()已經啟動,則不會調用onCreate()方法,點擊綁定按鈕之后調用onBind()方法;
2.服務未啟動,點擊啟動按鈕首先會調用oncreate()進行創建,然后通過onbind()進行綁定
注意:對於startService()啟動的服務,通過stopService()可以直接關閉服務,調用onDestory()方法;而對於進行了綁定的服務,則需要通過stopService()和unbindService()操作才能停止服務,停止服務,調用onUnbind()和onDestory()方法;
3.兩種啟動形式的區別
startService()和bindService()區別:
對於startService()這個方法啟動服務,活動不能對它進行操作;活動只能知道服務的啟動和暫停,對於服務運行的方法很難捕捉到;而對於bindService(),活動可以通過serviceConnection的實體類對onbind()返回的binder實現類實現的功能進行捕捉;