服务是一个在后台运行的组件,无需与用户交互即可执行长时间运行的操作,即使应用程序被销毁也能正常运行.服务基本上可以采用两种状态 :
Sr.No. | State&描述 |
---|---|
1 | Started 当应用程序组件(例如活动)通过调用 startService()启动它时,服务启动 .一旦启动,服务可以无限期地在后台运行,即使启动它的组件被销毁. |
2 | Bound 服务绑定当应用程序组件通过调用 bindService()绑定到它时.绑定服务提供了一个客户端 - 服务器接口,允许组件与服务交互,发送请求,获取结果,甚至跨进程间通信(IPC)进程. |
服务具有生命周期回调方法,您可以实施这些方法来监视服务状态的变化,并且您可以在适当的阶段执行工作.左侧的下图显示了使用startService()创建服务时的生命周期,右侧的图显示了使用bindService()创建服务时的生命周期:(图片提供:android.com)
要创建服务,您创建一个扩展Service基类或其现有子类之一的Java类.。Service 基类定义了各种回调方法,下面给出了最重要的回调方法.您不需要实现所有回调方法。但是,了解每一个并实现那些确保您的应用程序的行为符合用户期望的方式非常重要。
Sr.No. | Callback&说明 |
---|---|
1 | onStartCommand() 当另一个组件(例如活动)通过调用 startService()请求启动服务时,系统会调用此方法.如果实现此方法,则通过调用 stopSelf()或 stopService()方法,您有责任在工作完成后停止服务. |
2 | onBind() 当另一个组件想通过调用 bindService()与服务绑定时,系统调用此方法.如果实现此方法,则必须通过返回 IBinder 对象来提供客户端用于与服务通信的接口.您必须始终实现此方法,但如果您不想允许绑定,则应返回 null . |
3 | onUnbind() 当所有客户端与服务发布的特定接口断开连接时,系统会调用此方法. |
4 | onRebind() 系统在新客户端连接时调用此方法之前已通知其所有已断开连接的 onUnbind(意图). |
5 | onCreate() 系统首次使用 onStartCommand()或 onBind()创建服务时调用此方法.此调用是执行一次性设置所必需的. |
6 | onDestroy() 当服务不再使用且正在销毁时,系统会调用此方法.您的服务应该实现此操作以清理任何资源,如线程,注册的侦听器,接收器等. |
以下骨架服务演示了每个生命周期方法 :
- package com.it1352;
- import android.app.Service;
- import android.os.IBinder;
- import android.content.Intent;
- import android.os.Bundle;
- public class HelloService extends Service {
- /** indicates how to behave if the service is killed */
- int mStartMode;
- /** interface for clients that bind */
- IBinder mBinder;
- /** indicates whether onRebind should be used */
- boolean mAllowRebind;
- /** Called when the service is being created. */
- @Override
- public void onCreate() {
- }
- /** The service is starting, due to a call to startService() */
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- return mStartMode;
- }
- /** A client is binding to the service with bindService() */
- @Override
- public IBinder onBind(Intent intent) {
- return mBinder;
- }
- /** Called when all clients have unbound with unbindService() */
- @Override
- public boolean onUnbind(Intent intent) {
- return mAllowRebind;
- }
- /** Called when a client is binding to the service with bindService()*/
- @Override
- public void onRebind(Intent intent) {
- }
- /** Called when The service is no longer used and is being destroyed */
- @Override
- public void onDestroy() {
- }
- }
示例
此示例将指导您完成简单操作显示如何创建自己的Android服务的步骤。按照以下步骤修改我们在 Hello World示例章节中创建的Android应用程序:
Step | 描述 |
---|---|
1 | 您将使用Android StudioIDE创建一个Android应用程序,并将其命名为包裹下的我的应用程序 com.example.it13527.myapplication ,如 Hello World Example 章节中所述. |
2 | 修改主活动文件 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工作室负责字符串值 |
7 | 运行应用程序以启动Android模拟器并验证应用程序中所做更改的结果. |
以下是修改后的主活动文件的内容 MainActivity.java 的.该文件可以包括每个基本生命周期方法.我们添加了 startService()和 stopService()方法来启动和停止服务.
- package com.example.it13527.myapplication;
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.os.Bundle;
- import android.app.Activity;
- import android.util.Log;
- import android.view.View;
- public class MainActivity extends Activity {
- String msg = "Android : ";
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Log.d(msg, "The onCreate() event");
- }
- public void startService(View view) {
- startService(new Intent(getBaseContext(), MyService.class));
- }
- // Method to stop the service
- public void stopService(View view) {
- stopService(new Intent(getBaseContext(), MyService.class));
- }
- }
以下是 MyService.java 的内容.此文件可以根据需要实现与Service关联的一个或多个方法.现在我们只实现两个方法 onStartCommand()和 onDestroy() :
- package com.example.it13527.myapplication;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- import android.support.annotation.Nullable;
- import android.widget.Toast;
- /**
- * Created by TutorialsPoint7 on 8/23/2016.
- */
- public class MyService extends Service {
- @Nullable
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- // Let it continue running until it is stopped.
- Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
- return START_STICKY;
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- Toast.makeText(this, "Service Destroyed", 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.example.it13527.myapplication">
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- 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=".MyService" />
- </application>
- </manifest>
以下是 res/layout/activity_main.xml 文件的内容,包含两个按钮 :
- <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:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Example of services"
- android:layout_alignParentTop="true"
- android:layout_centerHorizontal="true"
- android:textSize="30dp" />
- <TextView
- android:id="@+id/textView2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Tutorials point "
- android:textColor="#ff87ff09"
- android:textSize="30dp"
- android:layout_above="@+id/imageButton"
- android:layout_centerHorizontal="true"
- android:layout_marginBottom="40dp" />
- <ImageButton
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/imageButton"
- android:src="@drawable/abc"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/button2"
- android:text="Start Services"
- android:onClick="startService"
- android:layout_below="@+id/imageButton"
- android:layout_centerHorizontal="true" />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Stop Services"
- android:id="@+id/button"
- android:onClick="stopService"
- android:layout_below="@+id/button2"
- android:layout_alignLeft="@+id/button2"
- android:layout_alignStart="@+id/button2"
- android:layout_alignRight="@+id/button2"
- android:layout_alignEnd="@+id/button2" />
- </RelativeLayout>
让我们尝试运行我们刚修改过的修改过的 Hello World!应用程序。我假设您在进行环境设置时创建了 AVD。要从Android工作室运行应用程序,请打开项目的一个活动文件,然后单击运行 icon从工具栏. Android Studio会在您的AVD上安装该应用并启动它,如果您的设置和应用程序一切正常,它将显示以下模拟器窗口 :
现在开始服务,点击启动服务按钮,这将启动服务,根据我们在 onStartCommand()方法中的编程,消息 Service Started 将出现在模拟器的底部,如下 :
要停止服务,可以单击"Stop Services"按钮。
转载本站内容时,请务必注明来自W3xue,违者必究。