Android(安卓)服务
服务(service)是一个在后台运行的组件,用于执行长时间运行的操作,而无需与用户交互,即使应用程序被破坏,它也能正常工作。
服务基本上可以有两种状态:
| 编号 | 状态 & 描述 |
|---|---|
| 1 | Started(启动) 当应用程序组件(如 activity)通过调用 |
| 2 | Bound(绑定) 当应用程序组件通过调用 |
服务具有生命周期回调方法,您可以实现这些方法来监视服务状态的更改,并可以在适当的阶段执行工作。左边的下图显示了使用 startService() 创建服务时的生命周期,右边的图显示了使用 bindService() 建立服务时的寿命周期:

要创建服务,您需要创建一个 Java 类来扩展服务基类或其现有子类之一。Service 基类定义了各种回调方法,下面给出了最重要的方法。您不需要实现所有回调方法。然而,重要的是你要理解每一个并实现那些确保你的应用程序按照用户期望的方式运行的东西。
| 编号 | 回调 & 描述 |
|---|---|
| 1 | onStartCommand() 当另一个组件(如 activity)通过调用 |
| 2 | onBind() 当另一个组件希望通过调用 |
| 3 | onUnbind() 当所有客户端都与服务发布的特定接口断开连接时,系统调用此方法。 |
| 4 | onRebind() 当新客户机连接到该服务时,系统会调用此方法,之前会通知它所有客户机都已在其 |
| 5 | onCreate() 首次使用 |
| 6 | onDestroy() 当服务不再使用并且正在被销毁时,系统将调用此方法您的服务应该实现这一点,以清理任何资源,如线程、注册的侦听器、接收器等。 |
以下框架服务演示了每个生命周期方法:
package com.cankaoshouce.helloworld;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class HelloService extends Service {/** 指示服务被终止时的行为 */int mStartMode;/** 绑定客户端的接口 */IBinder mBinder;/** 指示是否应使用onRebind */boolean mAllowRebind;/** 在创建服务时调用。 */@Overridepublic void onCreate() {}/** 由于调用startService(),服务正在启动 */@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {return mStartMode;}/** 客户端使用bindService()绑定到服务 */@Overridepublic IBinder onBind(Intent intent) {return mBinder;}/** 当所有客户端都与unbindService()解除绑定时调用*/@Overridepublic boolean onUnbind(Intent intent) {return mAllowRebind;}/** 当客户端通过bindService()绑定到服务时调用*/@Overridepublic void onRebind(Intent intent) {}/** 当服务不再使用并被销毁时调用 */@Overridepublic void onDestroy() {}}
实例
这个例子将带你通过简单的步骤来展示如何创建你自己的 Android 服务。
按照以下步骤修改我们在 Hello World 实例章节中创建的 Android 应用程序:
| Step | 描述 |
|---|---|
| 1 | 您将使用 Android Studio IDE 创建一个 Android 应用程序,并在com.example.cankaoshouce.myapplication 包下将其命名为 My application,如 Hello World 实例一章中所述。 |
| 2 | 修改主 activity 文件 MainActivity.java 来添加 startService() 和 stopService() 方法 |
| 3 | 在 com.example.My Application 包下创建一个新的 java 文件 MyService.java 该文件将实现与 Android 服务相关的方法 |
| 4 | 使用 <service … /> 标签在 AndroidManifest.xml 文件中定义您的服务。一个应用程序可以具有一个或多个服务,而没有任何限制。 |
| 5 | 修改 res/layout/activity_main.xml 文件的默认内容,以在线性布局中两个按钮。。 |
| 6 | 无需更改 res/values/strings.xml 文件中的任何常量。Android Studio 处理字符串值 |
| 7 | 运行该应用程序以启动 Android 模拟器并验证在该应用程序中所做更改的结果。。 |
以下是修改后的主要活动文件 MainActivity.java 的内容。该文件可以包括每个基本生命周期方法。我们添加了 startService() 和 stopService() 方法来启动和停止服务。
package com.cankaoshouce.helloworld;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;public class MainActivity extends AppCompatActivity {String msg = "Android : ";/** 在第一次创建 activity 时调用。 */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Log.d(msg, "onCreate() 事件被调用。");}public void startService(View view) {startService(new Intent(getBaseContext(), HelloService.class));}// 停止服务public void stopService(View view) {stopService(new Intent(getBaseContext(), HelloService.class));}}
以下是 HellowService.java 的内容。该文件可以根据要求实现与服务相关联的一种或多种方法。现在,我们将仅实现两种方法 onStartCommand() 和 onDestroy():
package com.cankaoshouce.helloworld;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.widget.Toast;import androidx.annotation.Nullable;public class HelloService extends Service {@Nullable@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {// Let it continue running until it is stopped.Toast.makeText(this, "服务启动。", Toast.LENGTH_LONG).show();return START_STICKY;}@Overridepublic void onDestroy() {super.onDestroy();Toast.makeText(this, "服务销毁。", Toast.LENGTH_LONG).show();}}
以下将修改 AndroidManifest.xml 文件的内容。在这里我们添加了 <service … /> 标签以包括我们的服务
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.cankaoshouce.helloworld"><applicationandroid: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><serviceandroid:name=".HelloService"android:enabled="true"android:exported="true"></service></application></manifest>
以下是 res/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"xmlns:app="http://schemas.android.com/apk/res-auto"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"><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:layout_marginTop="43dp"android:text="services 例子"android:textSize="30dp"tools:ignore="MissingConstraints"tools:layout_editor_absoluteX="109dp"tools:layout_editor_absoluteY="48dp" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/imageButton"android:layout_centerHorizontal="true"android:layout_marginBottom="96dp"android:text="参考手册 "android:textColor="#ff87ff09"android:textSize="30dp"tools:ignore="MissingConstraints"tools:layout_editor_absoluteX="162dp"tools:layout_editor_absoluteY="121dp" /><ImageButtonandroid:id="@+id/imageButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:src="@drawable/logo"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"tools:layout_editor_absoluteY="196dp"tools:ignore="MissingConstraints" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/imageButton"android:layout_centerHorizontal="true"android:layout_marginTop="16dp"android:onClick="startService"android:text="启动服务"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"tools:ignore="MissingConstraints"tools:layout_editor_absoluteY="397dp" /><Buttonandroid:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@+id/button2"android:layout_alignStart="@+id/button2"android:layout_alignLeft="@+id/button2"android:layout_alignEnd="@+id/button2"android:layout_alignRight="@+id/button2"android:layout_centerHorizontal="true"android:layout_marginStart="0dp"android:layout_marginLeft="0dp"android:layout_marginTop="47dp"android:layout_marginEnd="0dp"android:layout_marginRight="0dp"android:onClick="stopService"android:text="停止服务"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"tools:ignore="MissingConstraints"tools:layout_editor_absoluteY="326dp" /></RelativeLayout>
让我们尝试运行修改后的 Hello World!我们刚刚修改的应用程序。我假设您在进行环境设置时已创建了 AVD。要从 Android Studio 运行该应用,请打开您项目的活动文件之一,然后点击Android Studio 工具栏中的
图标。Android Studio 将应用程序安装在您的 AVD 上并启动它,如果设置和应用程序一切正常,它将显示在 "模拟器" 窗口下面。
现在启动服务,让我们单击 "启动服务" 按钮,这将启动服务,并且按照我们在 onStartCommand() 方法中的编程,一条 "服务启动。" 的消息将出现在模拟器的底部。
要停止服务,您可以单击停止服务按钮。