Android 中的 Service

Service简介:

Service是被设计用来在后台执行一些需要长时间运行的操作。Android由于允许Service在后台运行,甚至在结束Activity后,因此相对来说,Service相比Activity拥有更高的优先级。

创建Service:

要创建一个最基本的Service,需要完成以下工作:1)创建一个Java类,并让其继承Service 2)重写onCreate()和onBind()方法其中,onCreate()方法是当该Service被创建时执行的方法,onBind()是该Service被绑定时执行的方法。

public class ExampleService extends Service{
    
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

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

当创建了一个新的Service后,还必须在AndroidManifest.xml文件中对他进行配置,需要在application节点内包含一个Service标记。<service android:name=".ExampleService" android:enabled="true" android:permission="exam02.chenqian.com.servicedemo"></service>当然,如果你想要你自定义的Service仅能被自己编写的该应用程序使用,还可以在标签内添加:android:permission="exam02.chenqian.com.servicedemo"

让Service执行特定的任务:

如果想要Service执行特定的任务,可以复写Service的onStartCommand()方法,注意在API15之前为onStart()方法,现已不推荐,onStartCommand()方法的执行为该Service onCreate()之后。

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

启动和停止Service:

显式启动一个Service:

// 显示启动ExampleService
Intent intent = new Intent(this,ExampleService.class);
// 启动ExampleService
startService(intent);

为了方便观察,我们可以在之前创建的自定义的Service类中的onStartCommand()方法中添加Log.i("ServiceState","-------------->is Running");当我们从MainActivity调用运行时,可以在Logcat中观察到输出: I/ServiceState: is Running当然,我们也可以停止一个Service,为了让我们更清晰的观察到效果,我们可以在ExampleService类中复写onDestroy()方法:

@Override
    public void onDestroy() {
        Log.i("ServiceState","------------------>Destroy");
        super.onDestroy();
    }

可以在MainActivity中通过以下方式停止一个Service:显示停止一个Service:注意,写这里时更换了一个Service,并将该自定义的Service定位MyService,已经不是之前的ExampleService,不过您认可按照自己之前的继续编写,毕竟方法都是一样的;-)

//显示关闭Service
Intent serviceIntent = new Intent(MainActivity.this,MyService.class);
//关闭Service
stopService(serviceIntent);

注意Service的调用不可嵌套,因此无论Service被调用了多少次,对stopService()停止的一次调用就会终止它所匹配运行中的Service。由于Service具有较高的优先级,通常不会被运行时终止,因此可以通过自终止来避免后台运行Service耗费系统的资源。具体方法为在onStartCommand()方法中加入stopSelf();但是要注意的是这里的stopSelf()并不是直接终止Service,而是当Service的所有功能或请求执行完后,将Service停止掉,而不是等待系统回收,停止会调用onDestroy()销毁该Service。

将Service绑定到Activity:

当一个Service在一个Activity中被调用的时候,并不会随着Activity的销毁而销毁,而是仍有可能继续在后台运行着继续占用系统的资源,因此如果实现当Activity销毁时自动停止与其相关的服务,将会极大的节约系统的资源占用,我们可以通过以下方式实现Activity与Service的绑定:XML布局文件:在该布局文件中实现了四个按钮,分别执行启动Service、停止Service、绑定Service和解除绑定Service,清楚了吧:-)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="demo.chenqian.com.androidserverdemo.MainActivity">

    <!-- 开启Service -->
    <Button
        android:id="@+id/btnStartService"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="@string/startService"/>

    <!-- 关闭Service -->
    <Button
        android:id="@+id/btnStopService"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="@string/stopService"/>

    <!-- 绑定Service -->
    <Button
        android:id="@+id/btnBindService"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="@string/bindService"/>

    <!-- 解绑Service -->
    <Button
        android:id="@+id/btnUnbindService"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="@string/unbindService"/>

</LinearLayout>

MyService类:

package demo.chenqian.com.androidserverdemo;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;


public class MyService extends Service{
  /* 1、在下方我们定义了内部类MyBinder,这就是为什么我们这里现在能定义binder的原因    2、这里我们定义binder成员变量的目的是为了在下文的MainActivity中实现转型*/
    private MyBinder binder = new MyBinder();


    @Override
    public void onCreate() {
        Log.d("ServiceInfo","创建成功");
        super.onCreate();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d("ServiceInfo","绑定成功");
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("ServiceInfo","开始执行");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d("ServiceInfo","解绑成功");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        Log.d("ServiceInfo","销毁成功");
        super.onDestroy();
    }
  /*我们知道Android系统为了安全防护和整体的稳定性,将每一个应用程序隔离在相应的独立的“沙盒”之中,因此我们自定义的Service实际上是运行在    用户空间的,那么我们又有许多服务需要引用系统的Service,那么一个在用户空间一个系统空间,他们之间如何实现通信呢,这就需要Binder了,   Binder是Android系统中实现不同进程之间通信的一种方式,Binder本身有粘合剂的意思,Binder可以粘合Android系统中的四大组件,因此下方我     们在MyService类中新建了一个MyBinder内部类,并让其继承Binder类,用来实现对MyService的获取,这样,你该知道为什我们上文要新建一个My-    Binder的成员变量了吧 ^_^,在下方的MainActivity中你也可以看到相关实例的运用,例如public void onServiceConnected(ComponentName     name, IBinder service),注意这里的service是IBinder类型的,我们下方获取MyService将会用到他*/
    class MyBinder extends Binder{
        MyService getService(){
            Log.d("ServiceInfo","成功得到当前服务实例");
            return MyService.this;
        }
    }
}

MainActivity类:

package demo.chenqian.com.androidserverdemo;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Binder;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Context mContext;
    private Button btnStartService;
    private Button btnStopService;
    private Button btnBindService;
    private Button btnUnbindService;
    private MyService myService;
    private Intent serviceIntent;
    private boolean isBond;  /*isBond该变量用来标识当前的Activity与Service是否正在绑定,因为如果不进行标识,如果Activity没有    与Service进行绑定,而执行解除绑定的操作,会照成错误或抛出异常,因为当接触绑定时Android不允许绑定为null */    /*注意,这里我们新建了一个connection并重写了相关方法,为什么我们要新建这个连接,那是因为在下方的绑定和解绑      方法即bind和unbind需要一个connection。我们复写相关方法一是为了方便观察,另一个是为了在连接成功或失去关闭      连接时,执行相关的自定义的任务或操作*/
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("ServiceState","连接成功");
            myService = ((MyService.MyBinder)service).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d("ServiceState","关闭连接");       //当连接指向实例为null没有指引的连接的实例时,好被虚拟机回收,降低占用的资源
            myService = null;
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化数据
        mContext = this;
        isBond = false;
        //引入需要用到的组件
        btnStartService = (Button) findViewById(R.id.btnStartService);
        btnStopService = (Button) findViewById(R.id.btnStopService);
        btnBindService = (Button) findViewById(R.id.btnBindService);
        btnUnbindService = (Button) findViewById(R.id.btnUnbindService);
        //为按钮添加单击事件
        btnStartService.setOnClickListener(this);
        btnStopService.setOnClickListener(this);
        btnBindService.setOnClickListener(this);
        btnUnbindService.setOnClickListener(this);
    }


    @Override
    protected void onStart() {
        serviceIntent = new Intent(this,MyService.class);
        super.onStart();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnStartService:
                //开启Service
                startService(serviceIntent);
                break;
            case R.id.btnStopService:
                //关闭Service
                stopService(serviceIntent);
                break;
            case R.id.btnBindService:
                //绑定Service
                isBond = bindService(serviceIntent,connection,Context.BIND_AUTO_CREATE);
                break;
            case R.id.btnUnbindService:
                //解绑Service,当连接为null是解绑会报错
                if(isBond){
                    unbindService(connection);
                    //如果解绑成功,则修改连接标识为假
                    isBond = false;
                }
                break;
        }
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="demo.chenqian.com.androidserverdemo">

    <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" android:enabled="true" android:permission="demo.chenqian.com.androidserverdemo"></service>
    </application>

</manifest>

 关于以后:

1、感觉Binder那块还给大家解释的不太清楚,以后再深入研究下补充完整2、有时间会编写一个简单的后台播放音乐的实例提供给大家参考一下

&nbsp;