您的位置:首页 > 移动开发 > Android开发

Android 四大组件详解(二) Service

2016-01-21 11:01 453 查看
一开始学Android的时候就开始听说Service的相关内容,但是真正接触到Service并且使用它是在几个月前的一次IM(即时通讯)的实现上,IM的实现是基于Mqtt协议的,所以有一个Service专门来负责IM相关的操作,有空也会简单介绍一下Mqtt相关的知识。

Service是后台的概念,但它是运行在主线程的,这点很重要,千万别跟Thread搞混,也就是说如果你在Service中执行耗时操作的话是会带来ANR(应用无响应)错误的。Service跟Activity一样也有自己的生命周期,下面就让我们从以下几点来简单介绍一下Service的内容吧。

1. 如何使用Service

2. Service启动方式(bindService跟startService)

3. Service生命周期

4. IntentService简介

5.Service细节知识点

一、如何使用Service

启动一个Service之后Service就独立运行。

首先要自己生成一个Service类(这里用JaymeService为例)继承自Service,然后实现相关的方法(比如onCreate, onDestroy之类执行自己想要的操作)。

如果你想要启动Service时候Service跟启动者(Activity或者Application)之间保持联系。

那你就先要写一个自己的Binder类(这里以JaymeBinder为例),JaymeBinder必须继承自Binder类,然后在JaymeBinder中实现一个getService方法把Service本身返回,然后在JaymeService中生成一个JaymeBinder对象(这里叫为mBinder)。这样我们就有了一个mBinder,然后还要重写JaymeService中的onBind方法,把mBinder返回,Service中所要做的事情就是这些。(具体代码看下面!)

package com.scut.jayme.services;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

import com.scut.jayme.utils.LogUtils;

/**
* 一个用于掌握 Service生命周期 的Service
*
* Created by jayme on 15/12/5.
*/
public class JaymeService extends Service{
private JaymeBinder mBinder = new JaymeBinder();
@Override
public IBinder onBind(Intent intent) {
showMessage("onBind");
return mBinder;
}

@Override
public void onCreate() {
showMessage("onCreate");
super.onCreate();
}

@Override
public void onDestroy() {
showMessage("onDestroy");
super.onDestroy();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
showMessage("onStartCommand");
return super.onStartCommand(intent, flags, startId);
}

@Override
public boolean onUnbind(Intent intent) {
showMessage("onUnbind");
return super.onUnbind(intent);
}

@Override
public void onRebind(Intent intent) {
showMessage("onRebind");
super.onRebind(intent);
}

private void showMessage(String message){
LogUtils.i("jaymeService", message);
}

/**
* 用于跟Activity建立关联的Binder
*/
public class JaymeBinder extends Binder {
public JaymeService getService(){
return JaymeService.this;
}
}
}


在实现了Service之后,还要通过正确的方式启动Service才能保持启动者跟Service之间的联系(这里以MainActivity跟JaymeService绑定为例)。

首先在MainActivity中要生成一个ServiceConnection对象(具体代码如下)

private ServiceConnection mServiceConnection = new ServiceConnection() {
/**
* 当Service跟Activity绑定的时候会回调该函数
* @param name       名称
* @param binder     JaymeService中的mBinder对象
*/
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
LogUtils.i("jaymeActivity", "onServiceConnected");
/** 这里就获取到了Service对象,然后就可以操作Service了 */
mJaymeService = ((JaymeService.JaymeBinder)binder).getService();
}

@Override
public void onServiceDisconnected(ComponentName name) {
LogUtils.i("jaymeActivity", "onServiceDisconnected");
}
};


这样我们在MainActivity中就有了ServiceConnection对象,那就只差最后一步了,通过bindService方法绑定Service。

首先我们跟启动Activity类似,先生成一个Intent对象,然后调用bindService方法就可以实现Service跟MainActivity的绑定,由于此时MainActivity拥有了JaymeService的引用,也就可以操作JaymeService了,具体代码如下。

/**
* 绑定Service操作
*/
private void bindService(){
Intent bindIntent = new Intent(mContext, JaymeService.class);
bindService(bindIntent, mServiceConnection, BIND_AUTO_CREATE);
mIsBind = true;
}


最后再给出完整的MainActivity代码(包含后面要介绍的内容)

package com.scut.jayme.activitys;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.scut.jayme.R;
import com.scut.jayme.services.JaymeIntentService;
import com.scut.jayme.services.JaymeService;
import com.scut.jayme.utils.LogUtils;

public class MainActivity extends Activity implements View.OnClickListener{
private Context mContext;
private boolean mIsBind = false;
private JaymeService mJaymeService;

private Button mStartServiceButton;
private Button mStopServiceButton;
private Button mBindServiceButton;
private Button mUnbindServiceButton;
private Button mStartIntentServiceButton;

private ServiceConnection mServiceConnection = new ServiceConnection() { /** * 当Service跟Activity绑定的时候会回调该函数 * @param name 名称 * @param binder JaymeService中的mBinder对象 */ @Override public void onServiceConnected(ComponentName name, IBinder binder) { LogUtils.i("jaymeActivity", "onServiceConnected"); /** 这里就获取到了Service对象,然后就可以操作Service了 */ mJaymeService = ((JaymeService.JaymeBinder)binder).getService(); } @Override public void onServiceDisconnected(ComponentName name) { LogUtils.i("jaymeActivity", "onServiceDisconnected"); } };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = MainActivity.this;
findViews();
initListener();
}

/**
* 查找相关的控件
*/
private void findViews(){
mStartServiceButton = (Button)findViewById(R.id.main_btn_start_service);
mStopServiceButton = (Button)findViewById(R.id.main_btn_stop_service);
mBindServiceButton = (Button)findViewById(R.id.main_btn_bind_service);
mUnbindServiceButton = (Button)findViewById(R.id.main_btn_unbind_service);
mStartIntentServiceButton = (Button)findViewById(R.id.main_btn_start_intent_service);
}

/**
* 初始化监听器
*/
private void initListener(){
mStartServiceButton.setOnClickListener(this);
mStopServiceButton.setOnClickListener(this);
mBindServiceButton.setOnClickListener(this);
mUnbindServiceButton.setOnClickListener(this);
mStartIntentServiceButton.setOnClickListener(this);
}

/** * 绑定Service操作 */ private void bindService(){ Intent bindIntent = new Intent(mContext, JaymeService.class); bindService(bindIntent, mServiceConnection, BIND_AUTO_CREATE); mIsBind = true; }
/** * 解除绑定Service操作 */ private void unbindService(){ if(mIsBind) { unbindService(mServiceConnection); mIsBind = false; }else{ Toast.makeText(mContext, "Service 已经解除绑定", Toast.LENGTH_SHORT).show(); } }

/** * 启动Service操作 */ private void startService(){ Intent startIntent = new Intent(mContext, JaymeService.class); startService(startIntent); }

/**
* 停止Service操作
*/
private void stopService(){
Intent stopIntent = new Intent(mContext, JaymeService.class);
stopService(stopIntent);
}

/** * 启动 IntentService */ private void startIntentService(){ for(int i = 0; i < 5; i++) { Intent intent = new Intent(mContext, JaymeIntentService.class); intent.putExtra("time", System.currentTimeMillis()); startService(intent); } }

@Override
public void onClick(View v) {
String toastMessage = null;
switch (v.getId()){
case R.id.main_btn_start_service:
toastMessage = "start service";
startService();
break;

case R.id.main_btn_stop_service:
toastMessage = "stop service";
stopService();
break;

case R.id.main_btn_bind_service:
toastMessage = "bind service";
bindService();
break;

case R.id.main_btn_unbind_service:
toastMessage = "unbind service";
unbindService();
break;

case R.id.main_btn_start_intent_service:
toastMessage = "start IntentService";
startIntentService();
break;

default:
break;
}

if(null != toastMessage){
Toast.makeText(mContext, toastMessage, Toast.LENGTH_SHORT).show();
}
}
}


对应的layout文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView android:text="通过下面操作跟Log信息检查学习Service的生命周期" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bind"
android:id="@+id/main_btn_bind_service"
android:layout_below="@+id/textView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="44dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/main_btn_start_service"
android:layout_alignTop="@+id/main_btn_bind_service"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:id="@+id/main_btn_stop_service"
android:layout_below="@+id/main_btn_start_service"
android:layout_alignRight="@+id/main_btn_start_service"
android:layout_alignEnd="@+id/main_btn_start_service" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UNBIND"
android:id="@+id/main_btn_unbind_service"
android:layout_alignTop="@+id/main_btn_stop_service"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignLeft="@+id/main_btn_bind_service"
android:layout_alignStart="@+id/main_btn_bind_service" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="尝试三次启动IntentService"
android:id="@+id/main_btn_start_intent_service"
android:layout_below="@+id/main_btn_stop_service"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="129dp" />
</RelativeLayout>


二、Service的启动方式

上面花了比较大的篇幅去介绍如何使用Service,但是只是会用,具体Service的两种启动方式还是没有进行解释,下面就来简单解释一下启动Service的两种方式(startService跟bindService)。

startService方式启动Service

我们先来介绍简单的启动Service方式--startService,这种方法只要生成一个Intent对象,然后通过MainActivity的startService方法就可以启动一个Service(具体代码如下)。

/**
* 启动Service操作
*/
private void startService(){
Intent startIntent = new Intent(mContext, JaymeService.class);
startService(startIntent);
}


这种方式启动Service之后Service跟启动者(这里是MainActivity)没有什么关系,启动之后Service就会运行自己的生命周期,Service的销毁什么的都跟启动者没有关系了。

startService方式启动Service的关闭方法(stopService)

如果你想要关闭通过startService方式启动的Activity,你也可以通过生成一个Intent对象,然后调用Activity的stopService方法,具体方法如下。

/**
* 停止Service操作
*/
private void stopService(){
Intent stopIntent = new Intent(mContext, JaymeService.class);
stopService(stopIntent);
}


bindService方式启动Service

刚才我们介绍了比较简单的启动Service的方法,下面就来简单介绍一下绑定Service的方法,也就是用bindService方式启动Service。

这种方式启动Service的话就可以在Service跟启动者之间保持一定的联系,启动者也可以操作Service中的相关行为,这样就可以让一些后台的工作放到Service中,然后通过他们之间的关系来进行通信(比如IM中维持心跳连接的操作)。

上面也说了,要以这种方式启动Service,首先要生成一个ServiceConnection对象实现它的回调方法(也就是在回调方法中可以拿到Service中的相关引用达到操作Service的效果),然后在调用bindService的时候把ServiceConnection对象作为参数传进去就ok了,具体代码参看上面就好。

bindService启动方式启动Service的关闭方法

如果你想要关闭通过bindService方式启动的Service,你可以通过调用MainActivity中的stopService方法来关闭Service。这里要注意的一点就是你在调用unbindService的时候必须保证Service是处于bind的状态下的,不然会抛出异常(这也就是Main Activity 中mIsBind的作用)。下面具体看下代码

/**
* 解除绑定Service操作
*/
private void unbindService(){
if(mIsBind) {
unbindService(mServiceConnection);
mIsBind = false;
}else{
Toast.makeText(mContext, "Service 已经解除绑定", Toast.LENGTH_SHORT).show();
}
}


三、Service的生命周期

在介绍了Service的简单使用方式跟启动方式之后,就可以来探索一下Service的生命周期方法了,Service 的生命周期跟Activity的有一点点类似,话不多说,先上图,一切简单明了。



从图我们可以看出两种不同的启动方式Service的生命周期是不一样的(具体的生命周期演示最后会给出一个demo)。

通过startService方式启动

第一次启动调用onCreate->onStartCommand,再次启动当Service处于启动状态时只调用onStartCommand,然后Service自己关闭或者启动者把它关闭就进入onDestroy了。

通过bindService方式启动

第一次启动bindService(Service没有启动时)会调用onCreate->onBind,当已经bind的时候再启动bindService不触发任何回调,启动unbindService的时候会先调用onUnbind,如果此时Service没有跟别的启动者绑定就会进入onDestroy。

需要注意的地方:

当先启动startService还没有调用stopService的时候,此时第一次调用bindService跟unBindService没有任何异常,但当第二次或者更多次调用bindService、unbindService的时候会不触发onBind跟onUnbind(不知道这算不算一个bug,如果您知道希望指出)

四、IntentService的简介

如果你一开始认真看了这篇文章你就知道Service是运行在主线程的,所以要执行耗时操作的话也会带来ANR错误。而IntentService就是一种可以在里面执行耗时操作而不会带来ANR的Service。

具体方法就是自己写一个IntentService(这里使用JaymeIntentService为例)继承自IntentService,然后重写它的onHandleIntent函数,就可以在里面执行耗时的操作。

package com.scut.jayme.services;

import android.app.IntentService;
import android.content.Intent;

import com.scut.jayme.utils.LogUtils;

/**
* 用于测试 IntentService 的相关属性
* Created by jayme on 15/12/5.
*/
public class JaymeIntentService extends IntentService{
public JaymeIntentService() {
super("JaymeIntentService");
}

@Override
protected void onHandleIntent(Intent intent) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
long time = intent.getLongExtra("time", 0);
LogUtils.i("JaymeIntentService", time + "");
}
}


接着在启动者中调用startService就可以让IntentService执行耗时的操作了

/**
* 启动 IntentService
*/
private void startIntentService(){
for(int i  = 0; i < 5; i++) {
Intent intent = new Intent(mContext, JaymeIntentService.class);
intent.putExtra("time", System.currentTimeMillis());
startService(intent);
}
}


五、Service细节知识点

1. Service是运行在主线程的,所以不要在Service中执行耗时操作,不然会带来ANR错误,要执行耗时操作的话要在Service建立新的线程去执行
2. ServiceConnection中的onServiceConnected会在bindService的时候回调,但是onServiceDisconnected在unbindService的时候不一定会调用,而是在Service
被系统强杀或者Service崩溃了的时候会调用

总结:
我们在了解Service的时候首先要了解怎么使用Service,然后熟悉它的两种启动方式(startService跟bindService),熟悉它的生命周期,这样才能熟练地操作Service的相关行为。

补上LogUtils的源码,主要是为了发布版本控制Log输出的需要。

package com.scut.jayme.utils;

import android.util.Log;

/**
* Created by jay on 15/12/5.
*/
public class LogUtils {
private static final boolean SHOW_LOG = true;
private static final String TAG = "liujie";

public static void i(String message){
if(SHOW_LOG) {
Log.i(TAG, message);
}
}

public static void i(String tag, String message){
if(SHOW_LOG){
Log.i(tag, message);
}
}
}


六、Service演示demo



需要源码的可以去我的资源页查找下载(Android Studio源代码

* 上面的一切观点都是个人的理解,如果有不准确的地方还希望大家指出 *
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: