Android四大组件应用系列5——使用AIDL实现跨进程调用Service

一、问题描述

  Android应用程序的四大组件中Activity、BroadcastReceiver、ContentProvider、Service都可以进行跨进程。在上一篇我们通过ContentProvider实现了不同应用之间的跨进程调用,但ContentProvider主要是提供数据的共享(如sqlite数据库),那么我们希望跨进程调用服务(Service)呢?Android系统采用了远程过程调用(RPC)方式来实现。与很多其他的基于RPC的解决方案一样,Android使用一种接口定义语言(Interface Definition Language,IDL)来公开服务的接口。对于Service的跨进程调用需要通过AIDL来实现,AIDL服务应用非常广泛,如百度地图API中,就提供跨进程的服务,下面我们就看看如何实现AIDL Service ,案例如图:

二、实现AIDL服务的步骤

1.  编写AIDL文件

2.  如果aidl文件的内容是正确的,会自动生成一个Java接口文件(*.java)。

3.  建立一个服务类(Service的子类)。

4.  实现由aidl文件生成的Java接口。

5.  在AndroidManifest.xml文件中配置AIDL服务, 添加<action>标签的android:name,以便客户端使用隐式Intent启动服务

6、客户端

三、编写AIDL文件

  Android接口定义语言——LocalService.aidl

package com.jereh.remote;
interface LocalService{
        String getLocal();
}

IDE会自动生成LocalService.java 文件 如图所示:

 

四、Remote应用实现

  1、编写MyRemoteService

public class MyRemoteService extends Service {
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return new MyRemoteServiceImpl();
    }
    private class MyRemoteServiceImpl extends LocalService.Stub{
        @Override
        public String getLocal() throws RemoteException {
            // TODO Auto-generated method stub
            return "烟台杰瑞教育";
        }

    }
}

2、AndroidManifest.xml配置

<service android:name="com.jereh.retmote.MyRemoteService"
            android:process="remote"
            >
            <intent-filter>
                <action android:name="com.jereh.remote_service"/>
            </intent-filter>
  </service>
五、客户端实现

  1、在客户端应用中添加LocalService.aidl

  注意包名要与文件的在服务端定义的包名相同。如图所示:

 

同样会自动生成LocalService.java 代码

  2、MainActivity代码:

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void startService(View view){
        Intent service=new Intent("com.jereh.remote_service");
        super.bindService(service, conn, Context.BIND_AUTO_CREATE);
        
    }
    public void showInfo(View view){
        try {
            local=service.getLocal();
            Log.d("jereh", local);
            Toast.makeText(this, 
"您已进入"+local,Toast.LENGTH_LONG).show();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
private    LocalService service;
private    String local;
private ServiceConnection conn=new ServiceConnection() {
        @Override
        public void onServiceDisconnected(ComponentName arg0) {
        }
        @Override
        public void onServiceConnected(ComponentName name, IBinder binder) {
            // TODO Auto-generated method stub
            // 获取远程Service的onBinder方法返回的对象代理   
            service=LocalService.Stub.asInterface(binder);
        }
    };
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

xml文件:

<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:text="启动远程服务" android:onClick="startService" />
     <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="查看信息" android:onClick="showInfo" />
</LinearLayout>
作者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/ 
版权声明:本文版权归杰瑞教育技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
技术咨询:JRedu技术交流
 
原文地址:https://www.cnblogs.com/jerehedu/p/4919725.html