android绑定远程服务以及android接口定义语言(aidl)

本文详细介绍了在Android平台中如何实现进程间通信,重点阐述了使用AIDL接口进行跨进程对象访问的方法及步骤,包括服务端实现、接口定义、清单文件配置和客户端调用过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Andorid平台中,各个组件运行在自己的进程中,他们之间是不能相互访问的,但是在程序之间是不可避免的要传递一些对象,在进程之间相互通信。为了实现进程之间的相互通信,Andorid采用了一种轻量级的实现方式RPC(Remote Procedure Call 远程进程调用)来完成进程之间的通信,并且Android通过接口定义语言(Andorid Interface Definition Language ,AIDL)来生成两个进程之间相互访问的代码,例如,你在Activity里的代码需要访问Service中的一个方法,那么就可以通过这种方式来实现了。

   AIDL是Android的一种接口描述语言; 编译器可以通过aidl文件生成一段代码,通过预先定义的接口达到两个进程内部通信进程的目的. 如果需要在一个Activity中, 访问另一个Service中的某个对象, 需要先将对象转化成 AIDL可识别的参数(可能是多个参数), 然后使用AIDL来传递这些参数, 在消息的接收端, 使用这些参数组装成自己需要的对象。

   AIDL RPC机制是通过接口来实现的,类似Windows中的COM或者Corba,但他是轻量级的,客户端和被调用实现之间是通过代理模式实现的,代理类和被代理类实现同一个接口Ibinder接口。

下面是实现Activity访问Service例子的步骤:

远程服务段端:
com.example.remote.Myservice

package com.example.remote;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Toast;

public class Myservice extends Service {

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

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		System.out.println("服务创建");
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		System.out.println("服务结束");
		super.onDestroy();
	}
	public void methodInservice(String arr){
		
		System.out.println("我是远程服务方法,我被调用了");
	}
	private class MiddlePerson extends IMiddlePerson.Stub{

		@Override
		public void call(String string) throws RemoteException {
			// TODO Auto-generated method stub
			methodInservice(string);
		}

		
	}

}
编写aidl接口

  AIDL使用简单的语法来声明接口,描述其方法以及方法的参数和返回值。这些参数和返回值可以是任何类型,甚至是其他AIDL生成的接口。重要的是必须导入导入除了内建类型(例如:int,boolean等)外的任何其他类型,哪怕是这些类型是在与接口相同的包中。具体的要求如下:

  • JAVA基本数据类型不需要导入
  • String,List,MapCharSequence不需要导入
package com.example.remote;

 interface IMiddlePerson {
	void call(String string);
}

其次在清单文件中声明服务:

 

<service  android:name="com.example.remote.Myservice">
          <intent-filter ><action android:name="com.example.remote.Myservice"/></intent-filter>  
        </service>

二:编写调用远程服务的程序:

1:将被调用服务的aidl文件copy过来,注意,报名必须一致

调用端布局文件

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_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="mystop"
        android:text="解除绑定" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="call"
        android:text="调用方法" />
</LinearLayout>

调用实现:

package com.example.bindremote;




import com.example.remote.IMiddlePerson;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends Activity {
	private IMiddlePerson im;
	private Myconn conn;
	   @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_main);
	    }
	 
	 public void mystop(View vew){
		unbindService(conn);
	}
	 public void bind(View vew){
		 Intent service=new Intent();
		 service.setAction("com.example.remote.Myservice");
		 conn=new  Myconn();
	 	bindService(service, conn, BIND_AUTO_CREATE);
	 
	 
	 }
	 public void call(View vew){
		 try {
			im.call("leigewudi");
			System.out.println(im);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
	public class Myconn implements ServiceConnection {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			// TODO Auto-generated method stub
			im=IMiddlePerson.Stub.asInterface(service);
			
			System.out.println("服务被调用了"+im);
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			unbindService(conn);
			System.out.println("服务结束");
		}
		
	}
}

实验结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值