跨进程调用Service(AIDL 服务)

本地Service的onBind()方法会直接把IBinder对象本身传给客户端的ServiceConnection的onServiceConnected方法的第二个参数。

远程Service的onBind()方法只是将IBinder对象的代理传递给客户端的ServiceConnection的onServiceConnected方法的第二个参数。

定义好ICat.aidl后,自动生成ICat.java接口,在该接口内包含一个Stub内部类,该内部类实现了IBinder、ICat两个接口,这个Stub类将会作为远程Service的回调类---它实现了IBinder接口,因此可作为Service的onBind()方法的返回值。

// Service 1
package org.crazyit.service;
interface ICat
{
	String getColor();
	double getWeight();
}
// Service 2
public class AidlService extends Service {
    private CatBinder mCatBinder;

    Timer mTimer = new Timer();

    String[] mColors = new String[] {
        "红色", "黄色", "黑色"
    };

    double[] mWeights = new double[] {
        2.3, 3.1, 1.58
    };

    private String mColor;

    private double mWeight;

    // 继承Stub,也就是实现额ICat接口,并实现了IBinder接口
    public class CatBinder extends ICat.Stub {
        @Override
        public String getColor() throws RemoteException {
            return mColor;
        }

        @Override
        public double getWeight() throws RemoteException {
            return mWeight;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        // 
        mCatBinder = new CatBinder();
        //
        mTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                // 随机地改变Service组件内color、weight属性的值。
                int rand = (int) (Math.random() * 3);
                mColor = mColors[rand];
                mWeight = mWeights[rand];
                System.out.println("--------" + rand);
            }
        }, 0, 800);
    }

    @Override
    public IBinder onBind(Intent arg0) {
        /*
         * 返回catBinder对象 
         * 在绑定本地Service的情况下,该catBinder对象会直接传给客户端的ServiceConnection对象的onServiceConnected方法的第二个参数;
         * 在绑定远程Service的情况下,只将catBinder对象的代理传给客户端的ServiceConnection对象的onServiceConnected方法的第二个参数;
         */
        return mCatBinder;
    }

    @Override
    public void onDestroy() {
        mTimer.cancel();
    }
}
// Service 3
<service android:name=".AidlService" >
    <intent-filter>
        <action android:name="org.crazyit.aidl.action.AIDL_SERVICE" />
    </intent-filter>
</service>
注意:将该应用部署到手机上,在程序列表看不到这个应用,因为该应用并未提供Acitvity。但没关系,该应用提供的Service可以供其他应用程序来调用。

将Service端的AIDL接口文件复制到客户端
// Client 1
package org.crazyit.service; // 和服务端的在一个包名下
interface ICat
{
	String getColor();
	double getWeight();
}
// Client 2
public class AidlClient extends Activity {
    private ICat mCatService;

    private Button get;

    EditText color, weight;

    private final ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // 与绑定本地Service不同的是,绑定远程Service的ServiceConnection并不能直接获取Service的onBind()方法所返回的对象,它只能返回onBind()方法所返回的对象的代理,因此通过如下代码实现。
           // 获取远程Service的onBind方法返回的对象的代理
            mCatService = ICat.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mCatService = null;
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        get = (Button) findViewById(R.id.get);
        color = (EditText) findViewById(R.id.color);
        weight = (EditText) findViewById(R.id.weight);

        // 创建所需绑定的Service的Intent
        Intent intent = new Intent();
        intent.setAction("org.crazyit.aidl.action.AIDL_SERVICE");

        // 绑定远程Service
        bindService(intent, mConnection, Service.BIND_AUTO_CREATE);

        get.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                try {
                    // 获取、并显示远程Service的状态
                    color.setText(mCatService.getColor());
                    weight.setText(mCatService.getWeight() + "");
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 解除绑定
        this.unbindService(mConnection);
    }
}

Android要求调用远程Service的参数和返回值都必须实现Parceable接口。实现Parceable接口不仅要求实现该接口里定义的方法,而且要求在实现类中定义一个名为CREATOR、类型为Parcelable.Creator的静态Field。除此之外,还要求使用AIDL代码来定义这些自定义类型。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值