Android-ServiceConnection

在Activity与Service 交互的时候,应用组件可以通过调用绑定到一个绑定成功之后系统之后调用的onBind()它返回一个用来与交互的IBinder

这个时候问题来了,绑定的过程是异步的.bindService()会立即返回,它不会返回IBinder给客户端.

bindService(intent, mConnection, Context.BIND_AUTO_CREATE);

 @Override
    public boolean bindService(Intent service, ServiceConnection conn,
            int flags) {
        return mBase.bindService(service, conn, flags);
    }

这里可以看到源码实现里面返回的是只是一个标识绑定是否成功,却未返回IBinder对象, 这里它接受的参数里面有个ServiceConnection 对象,打开ServiceConnction源码

package android.content;

import android.os.IBinder;

/**
 * Interface for monitoring the state of an application service.  See
 * {@link android.app.Service} and
 * {@link Context#bindService Context.bindService()} for more information.
 * <p>Like many callbacks from the system, the methods on this class are called
 * from the main thread of your process.
 */
public interface ServiceConnection {
    /**
     * Called when a connection to the Service has been established, with
     * the {@link android.os.IBinder} of the communication channel to the
     * Service.
     * 绑定建立成功后.系统通过该方法回调传递IBinder给组件
     * @param name The concrete component name of the service that has
     * been connected.
     *
     * @param service The IBinder of the Service's communication channel,
     * which you can now make calls on.
     */
    public void onServiceConnected(ComponentName name, IBinder service);

    /**
     * Called when a connection to the Service has been lost.  This typically
     * happens when the process hosting the service has crashed or been killed.
     * This does <em>not</em> remove the ServiceConnection itself -- this
     * binding to the service will remain active, and you will receive a call
     * to {@link #onServiceConnected} when the Service is next running.
     *<span style="font-size: 14px; line-height: 26px; font-family: 'Times New Roman', serif;"><span lang="en-US">Android</span></span><span style="font-family: Arial; font-size: 14px; line-height: 26px;">系统在同</span><span style="font-size: 14px; line-height: 26px; font-family: 'Times New Roman', serif;"><span lang="en-US">service</span></span><span style="font-family: Arial; font-size: 14px; line-height: 26px;">的连接意外丢失时调用这个.比如当</span><span style="font-size: 14px; line-height: 26px; font-family: 'Times New Roman', serif;"><span lang="en-US">service</span></span><span style="font-family: Arial; font-size: 14px; line-height: 26px;">崩溃了或被强杀了.当客户端解除绑定时,这个方法不会被调用</span>
     * @param name The concrete component name of the service whose
     * connection has been lost.
     */
    public void onServiceDisconnected(ComponentName name);
}

所以要接收IBinder,客户端必须创建一个ServiceConnection的实例并传给bindService()ServiceConnection包含一个回调方法,系统调用这个方法来传递要返回的IBinder

所以绑定接收IBinder就分以下2步

1,创建ServiceConnection 对象,在回调方法onServiceConnected中接受IBinder参数 

2.在bindService中传入ServiceConnection 对象;

Bundle args = new Bundle();
Intent intent = new Intent(mAIDLActivity.this,mAIDLService.class);intent.putExtras(args);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
startService(intent);
后面会具体分析它的回调过程,待续


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值