Service是Android四大组件中与Activity最相似的组件,它们都代表可执行的程序。Service与Activity的区别在于:Service一直在后台运行,它没有用户界面,所以绝对不会到前台来。一旦Service被启动起来之后,它就与Activity一样。它完全具有自己的生命周期。关于程序中Activity与Service的选择标准是:如果某个程序组件需要在运行时向用户呈现某种界面,或者该程序需要与用户交互,就需要使用Activity,否则就应该考虑使用Service了。
Android系统中运行Service有如下两种方式:
1.StartService()
通过该方法启动Service,访问者与Service之间没有关联,即使访问者退出了,Service仍然运行。service和访问者之间无法进行通信、数据交换。
2.bindService()
通过该方法启动Service,访问者与Service绑定在了一起,访问者一旦退出,Service也就终止。ServiceConnection对象用于监听访问者与Service之间的连接情况。当访问者与Service之间连接成功时,将毁掉该ServiceConnection对象的onServiceConnected方法。该方法中有一个IBinder对象,该对象即可实现与被绑定Service之间的通信。
当开发Service类时,该Service类必须提供一个IBinder onBind(Intent intent)方法,在绑定本地Service的情况下,onBind(Intent intent)方法所返回的IBinder对象将会传给ServiceConnection对象里onServiceConnected(ComponentName name, IBinder service)方法的service参数,这样访问者就可以通过该IBinder对象与Service进行通信。
onServiceDisconnected() 在连接正常关闭的情况下是不会被调用的, 该方法只在Service 被破坏了或者被杀死的时候调用. 例如, 系统资源不足, 要关闭一些Services, 刚好连接绑定的 Service 是被关闭者之一, 这个时候onServiceDisconnected() 就会被调用。
测试代码如下:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.maxus.servicetest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".BindService"></service>
<service android:name=".StartService"></service>
</application>
</manifest>
StartService.java
package com.maxus.servicetest;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
public class StartService extends Service {
private final String TAG = "ServiceTest";
@Override
public void onCreate() {
Log.i(TAG, "StartService#onCreate");
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "StartService#onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.i(TAG, "StartService#onDestory");
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
BindService.java
package com.maxus.servicetest;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
public class BindService extends Service {
private static final String TAG = "ServiceTest";
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "BindService#onCreate");
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "BindService#onBind");
return new MyBinderService();
}
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "BindService#onUnbind");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "BindService#onDestroy");
}
public void testFunc() {
Log.i(TAG, "BindService#testFunc");
}
public class MyBinderService extends Binder {
public MyBinderService() {
super();
}
public BindService getBinderService() {
return BindService.this;
}
}
}
MainActivity.java
package com.maxus.servicetest;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private final String TAG = "ServiceTest";
ServiceConnection conn;
BindService mBindService;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "MainActivity#onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "ServiceConnection#onServiceConnected");
mBindService = ((BindService.MyBinderService) service).getBinderService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.i(TAG, "ServiceConnection#onServiceDisconnected");
mBindService = null;
}
};
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mBindService != null) {
unbindService(conn);
}
}
public void onClick(View v) {
Log.i(TAG, "MainActivity#onClick Id = " + v.getId());
Intent intent;
switch (v.getId()) {
case R.id.bindService:
intent = new Intent(this, BindService.class);
bindService(intent, conn, Service.BIND_AUTO_CREATE);
break;
case R.id.startService:
intent = new Intent(this, StartService.class);
startService(intent);
break;
case R.id.testFunc:
if (mBindService != null) {
mBindService.testFunc();
}
break;
case R.id.unbindService:
if (mBindService != null) {
unbindService(conn);
}
break;
default:
break;
}
}
}