进程间的通信AIDL与远程服务的访问

一、简介

1.不明思议,一个是提供服务端,一个是需要服务端,但是与本地服务不同的是,他们处于两个不同进程之间;可以开启服务,但是不能调用远程服务内的方法(内部类不是实现同一个接口,不能访问);而AIDL正是解决这一问题的机制,共享接口。

二、提供服务端部署

2.自定义远程服务端工程结构图

这里写图片描述

a.新建LeadService.java

/**
 * @author Chixi
 * 
 * @version 创建时间 : 2016-7-31 下午4:23:01
 * 
 */
public class LeadService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        System.out.println("绑定LeadService...");
        return new ZhouMi();
    }

    /**
     * 服务与activity中间人
     * 
     * @author QiShui
     * 
     * aidl android interface definition language
     */
    class ZhouMi extends Stub {

        public void deal() {
            getCard();
        }
    }

    public void getCard() {

        System.out.println("李局为你办证...");

    }

}

b.其中Stub正是来自gen中PublicInterFace.java中,已经继承了Binder,和实现PublicInterFace.aidl方法(默认是公共的public,不需要再添加修饰词)。

/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements org.qishui.service.PublicInterFace
{
private static final java.lang.String DESCRIPTOR = "org.qishui.service.PublicInterFace";

c.在清单文件中注册自定义service

 <!-- 自定义动作,用于远程连接 -->
        <service android:name="org.qishui.service.LeadService" >
            <intent-filter>
                <action android:name="qishuichixi.action.com" >
                </action>
            </intent-filter>
        </service>

注意:服务端在另一个被需要被服务的进程前启动即可

三、被提供服务端,接受服务部署

a.文档结构图

这里写图片描述

b.在布局文件中设置两个点击按钮

<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" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="bind"
        android:text="绑定服务" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="deal"
        android:text="办证服务" />

</LinearLayout>

c.绑定服务,初始化并获取内部类对象,调用远程服务中的方法。

/**
 * 启动远程服务
 * @author QiShui
 *
 */
public class MainActivity extends Activity {

    PublicInterFace face;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
/**
 * 绑定服务
 * @param view
 */
    public void bind(View view) {
        Intent intent = new Intent();
        /**
         * 自定义action一致,不能不能绑定service
         */
        intent.setAction("qishuichixi.action.com");
        /**
         * 转换成系统服务,提高生命力,也可以不用转成系统进程,若需要,就要在bind之前。
         */
        startService(intent);

        bindService(intent, new MyConn(), BIND_AUTO_CREATE);
    }
    /**
     * 绑定服务中的连接对象
     * @author QiShui
     *
     */
    class MyConn implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            // TODO Auto-generated method stub
            face=Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            // TODO Auto-generated method stub

        }

    }


    public void deal(View view) {

        Intent intent = new Intent();
        intent.setAction("qishuichixi.action.com");
        /**
         * 对连接远程服务异常捕获处理
         */
        try {
            face.deal();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


}

d.处理aidl文件时,包名,类名与服务端一致即可。

e.测试效果

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值