Android服务Service详解(作用,生命周期,AIDL)系列文章--绑定服务

来源:互联网 发布:数据的一致性是指 编辑:程序博客网 时间:2024/05/21 18:31

Android服务Service详解(作用,生命周期,AIDL)系列文章--绑定服务


前面我们只是简单地了解了服务的生命周期,其实呀,服务的生命周期没有那么简单。



在学习服务的所有生命周期之前,我们首先要学会服务的另外一种启动方式,绑定服务。

这里的话,也是简单地通过一个例子来给同学们做一个演示,其实呢,后面我都会把这些博客讲成视频的,只是现在时间 还不够,我需要准备一下,如果你求欢的话,或者没有看懂的。就等我出视频吧!!

首先,我们还是和前面一样,通过代码来说明如何去绑定服务,绑定服务到底是用来做什么的呢?

同样,还是前面的示例代码,还是服务SOBTestService这个服务。

不一样的是,我们在我们的MainActivity的主界面上添加了两个按钮,一个是绑定服务,一个是解绑服务,代码如下:

<?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:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity">     <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="start"        android:text="开启服务"/>     <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="stop"        android:text="停止服务"/>     <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="bind"        android:text="绑定服务"/>     <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:onClick="unbind"        android:text="解绑服务"/></LinearLayout>



然后,我们看回上次SOBTestService的代码,里面我们发现,有一个方法是:

@Nullable  @Override  public IBinder onBind(Intent intent) {      return null;  }

对吧,这个方法是onBind方法,它呢,无论那个类去继承自己Service都要实现这个方法。其实呀,这个方法就是一个回调。服务绑定时的回调。

问题来了:为什么要绑定服务呢?

前面,我们只是启动服务,停止服务,其实呢,我们并没能和服务进行交流。不管是数据上的交流还是情感上的交流都没有,对吧!

所以呢,我们需要通过绑定服务。而服务被绑定以后呢,它的内部需要返回一个对象是吧,也就是onBind的这个回调,它的内部需要返回一个对象。这个对象呢,我们就可以操作服务了。因此,我们绑定服务,就是为了和服务进行交流,调用服务内部的方法。
下面我们就去绑定方法,并且调用一下服务内部的一个方法。

package com.sunofbeaches.servicetestdemo; 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; /** * Created by TrillGates on 17/4/15. * God bless my code! */public class SOBTestService extends Service {     private static final String TAG = "SOBTestService";      //继承自Binder,其实Binder就是IBinder的一个子类嘛。    public class CommunicationBinder extends Binder {         //这个方法供外部调用,用于调用内部的方法。        public void callInnerMethod() {            //调用内部的方法            innerMethod();        }     }      @Nullable    @Override    public IBinder onBind(Intent intent) {        //绑定以后,反回CommunicationBinder        return new CommunicationBinder();    }     @Override    public void onCreate() {        super.onCreate();        Log.d(TAG, "服务被创建了....");    }     @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.d(TAG, "执行了 onStartCommand...");        return super.onStartCommand(intent, flags, startId);    }     @Override    public void onDestroy() {        super.onDestroy();        Log.d(TAG, "服务被销毁了....");    }     private void innerMethod() {        Log.d(TAG, "我是服务内部的方法被调用啦...");    }}
首先,我们创建了一个内部类,继承自己Binder这个类,其实呢,Binder这个类就是IBinder的子类,需要返回IBinder类型,由JAVA的多态,我们可以返回它的孩子。
所以这里话,我们在它绑定的回调里头,创建一个CommunicationBinder,这个Binder有一个方法,就是调用内部的方法。服务内部的方法。就是这么简单。

接着,我们来看看我们MainActivity是怎么写的呢:

package com.sunofbeaches.servicetestdemo; import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View; public class MainActivity extends AppCompatActivity {     private static final String TAG = "MainActivity";     @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);     }     /**     * 开始服务     *     * @param view     */    public void start(View view) {        Intent intent = new Intent(this, SOBTestService.class);        startService(intent);    }     /**     * 停止服务     *     * @param view     */    public void stop(View view) {        Intent intent = new Intent(this, SOBTestService.class);        stopService(intent);    }      /**     * 绑定服务     *     * @param view     */    public void bind(View view) {        Log.d(TAG, "点击绑定服务按钮..");         Intent intent = new Intent(this, SOBTestService.class);         /**         * 这里面需要三个参数,第一个是意图对象,第二个是回调,第三个是创建模式。         */        bindService(intent, mConnection, BIND_AUTO_CREATE);    }     private ServiceConnection mConnection = new ServiceConnection() {        @Override        public void onServiceConnected(ComponentName name, IBinder service) {            //这里呢,则是绑定上了,那到我们输出它的名字,然后调用它的方法即可。            Log.d(TAG, "services name == " + name);             //调用内部方法            ((SOBTestService.CommunicationBinder) service).callInnerMethod();        }         @Override        public void onServiceDisconnected(ComponentName name) {             Log.d(TAG, "服务断开连接....");        }    };     /**     * 解除服务绑定...     */    public void unbind(View view) {        Log.d(TAG, "点击了解绑服务按钮..");        unbindService(mConnection);    } }
上面的代码,其实其他已经看过了,主要的就两个,一个是绑定服务,一个是解绑服务。
在绑定服务里头,我们需要三个参数,一个是意图对象,第二个是一个回调接口,用于通知绑定成功,和绑定解除的。
第三个参数是服务的创建模式,这里我们填写自动创建即可。

当我们运行起来,点击绑定服务,执行结果如下:




我们可以得知,执行了onCreate方法,但是没有执行onStartCommand方法。
同时,也执行了内部方法的调用,说明我们是调用成功的,是吧!其实呀,这只是一个代理模式!

然后我们再点击一下解绑服务:


到这里的话,服务解除绑定了,也销毁了。

现在,知道为什么服务的生命周期比较多样了吧。后面的话,我们会详细进行说明,对各种情况的生命周期给各们同学捊一次!

本文支持现金打赏哦!点个赞也行嘛!

网易云视频:



社区网站:




阅读全文
0 0