绑定本地Service并与之通信

当程序通过startService()和stopService()启动、关闭Service时,Service与访问者之间基本上不存在太多的关联,因此Service和访问者之间也无法进行通信、数据交换。

如果Service和访问者之间需要进行方法调用或数据交换,则应该使用bindService()和unBindService()方法启动、关闭Service。

Context的bindService()方法的完整方法签名为:bindService(Intent service, ServiceConnection conn, int flags),该方法的三个参数的解释如下:

  • service
    该参数通过Intent指定要启动的Service。

  • conn
    该参数是一个ServiceConnection对象,该对象用于监听访问者于Service之间的连接情况。当访问者与Service之间连接成功时将回调该ServiceConnection对象的onServiceConnected(ComponentName name, IBinder service)方法;当Service所在的宿主进程由于异常终止或由于其他原因终止,导致该Service与访问者之间断开连接时会回调该ServiceConnection对象的onServiceDisconnected(ComponentName name)方法。
    注意:当调用者主动通过unBindService()方法断开与Service的连接时,ServiceConnection对象的onServiceDisconnected(ComponentName name)方法并不会被调用。

  • flags
    指定绑定时是否自动创建Service(如果Service还未创建)。该参数可指定为0(不自动创建)或BIND_AUTO_CREATE(自动创建)。

注意到ServiceConnection对象中的onServiceConnected方法中有一个IBinder对象,该对象即可实现与被绑定Service之间的通信。

当开发Service类时,该Service类必须提供一个IBinder onBind(Intent intent)方法,在绑定本地Service的情况下,onBind(Intent intent)方法所返回的IBinder对象将会传给ServiceConnection对象里onServiceConnected(ComponentName name, IBinder service)方法的Service对象,这样访问者就可通过该IBinder对象与Service进行通信。

IBinder对象相当于Service组件的内部钩子,该钩子关联到绑定的Service组件,当其它程序组件绑定该Service时,Service将会把IBinder对象返回给其它程序组件,其它程序组件通过给IBinder对象即可与Service组件进行实时通信。

实际上开发时通常会采用继承Binder(IBinder的实现类)的方法实现自己的IBinder对象。


下面程序示范如何在Activity中绑定本地Service,并获取Service的运行状态。

package com.mystudy.kibi.service;

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

/**
 * Created by KIBI熊 on 16/6/28.
 */
public class TestService extends Service{

    private MyBinder myBinder = new MyBinder();
    private boolean run;
    private int count;

    @Override
    public IBinder onBind(Intent intent) {
        Log.e("==>","onBind");
        return myBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("==>","onCreate");
        run = true;
        count = 0;
        new Thread(){
            @Override
            public void run() {
                while (run){
                    try {
                        sleep(1000);
                        count++;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("==>","onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("==>","onUnbind");
        return super.onUnbind(intent);
    }

    @Override
    public void onDestroy() {
        Log.e("==>","onDestroy");
        super.onDestroy();
        run = false;
    }

    public class MyBinder extends Binder{
        public int getCount(){
            return count;
        }
    }
}

上面Service类的onBind()方法返回一个可访问该Service状态数据(count值)的IBinder对象,该对象将传给该Service的访问者。

配置文件:

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

    <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="com.mystudy.kibi.service.TestService">
            <intent-filter>
                <action android:name="com.mystudy.kibi.service.TEST_SERVICE" />
            </intent-filter>
        </service>

    </application>

</manifest>

接下来定义一个Activity来绑定本地Service,并在该Activity中通过MyBinder对象访问Service的内部状态。该Activity的界面上包含三个按钮,第一个用于绑定Service,第二个用于解除绑定,第三个按钮用于获取Service的运行状态。代码如下:

package com.mystudy.kibi.networktype;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
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 com.mystudy.kibi.service.TestService;

public class MainActivity extends AppCompatActivity {

    private Button bindBtn,unBindBtn,getBtn;
    private TestService.MyBinder binder;
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder = (TestService.MyBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e("==>","onServiceDisconnected");
        }
    };
    private final Intent intent = new Intent();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();
        initView();
        initListener();

    }

    private void initView(){
        bindBtn = (Button) findViewById(R.id.bind_btn);
        unBindBtn = (Button) findViewById(R.id.unbind_btn);
        getBtn = (Button) findViewById(R.id.get_btn);
    }

    private void initListener(){
        bindBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindService(intent,serviceConnection, Service.BIND_AUTO_CREATE);
            }
        });
        unBindBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unbindService(serviceConnection);
            }
        });
        getBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("==>",binder.getCount()+"");
            }
        });
    }

    private void initData(){
        intent.setAction("com.mystudy.kibi.service.TEST_SERVICE");
        intent.setPackage(getPackageName());
    }

}

布局文件:

<?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"
    android:gravity="center_horizontal"
    tools:context="com.mystudy.kibi.networktype.MainActivity">

    <Button
        android:layout_marginTop="50dp"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="bind"
        android:textColor="#888"
        android:id="@+id/bind_btn" />

    <Button
        android:layout_marginTop="50dp"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="unbind"
        android:textColor="#888"
        android:id="@+id/unbind_btn" />

    <Button
        android:layout_marginTop="50dp"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="get"
        android:textColor="#888"
        android:id="@+id/get_btn" />
</LinearLayout>

测试:
点击绑定按钮多次之后再点击解除绑定按钮,测试结果:

07-03 13:45:48.196 21228-21228/com.mystudy.kibi.networktype E/==>: onCreate
07-03 13:45:48.199 21228-21228/com.mystudy.kibi.networktype E/==>: onBind
07-03 13:45:51.693 21228-21228/com.mystudy.kibi.networktype E/==>: onUnbind
07-03 13:45:51.693 21228-21228/com.mystudy.kibi.networktype E/==>: onDestroy

点击绑定按钮之后,点击获取Service状态按钮,再点击解除绑定按钮,测试结果:

07-03 13:49:52.089 22837-22837/com.mystudy.kibi.networktype E/==>: onCreate
07-03 13:49:52.094 22837-22837/com.mystudy.kibi.networktype E/==>: onBind
07-03 13:49:53.622 22837-22837/com.mystudy.kibi.networktype E/==>: 1
07-03 13:49:56.065 22837-22837/com.mystudy.kibi.networktype E/==>: 3
07-03 13:49:58.140 22837-22837/com.mystudy.kibi.networktype E/==>: 6
07-03 13:50:00.288 22837-22837/com.mystudy.kibi.networktype E/==>: onUnbind
07-03 13:50:00.288 22837-22837/com.mystudy.kibi.networktype E/==>: onDestroy

正如测试结果所示,当程序调用unbindService()方法解除对某个Service的绑定时,系统会先回调该Service的onUnbind()方法,然后再回调onDestroy()方法。

与多次调用startService()方法启动Service不同的是,多次调用bindService()方法并不会执行反复绑定。对于开启、停止Service示例中每点击一次启动Service按钮,系统就会回调一次onStartCommand() 方法;对于这个示例,不管用户点击绑定按钮多少次,系统只会回调Service的onBind() 方法一次。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值