您的位置:首页 > 大数据 > 人工智能

安卓Service组件使用系列6:使用AIDL完成两个进程间的通信

2016-03-08 13:39 651 查看
Android Interface Definition Language (AIDL) 安卓接口定义语言,定义这样的接口,必须以.aidl作为后綴名。使用这样的接口定义,用于两个进程间的通信(两个apk之间需要一个通信功能时用到)。这时需要定义.aidl文件。下面我们来看一下它的使用方法。

整体思路:创建一个安卓工程android_service_aidl_server,作为服务器端,定义一个MyService类,继承Service,定义一个binder对象,重写getData方法,在这个方法中根据参数arg的不同返回不同的结果。定义一个名为DataService.aidl的文件作为安卓接口声明接口中的getData方法。创建另一个安卓工程android_service_aidl_client,作为客户端端,定义一个与第一个工程相同的名为DataService.aidl的文件,在xml'文件中放置两个Button控件,定义一个ServiceConnection对象,在这个对象的onServiceConnected方法中实例化DataService对象,设置两个Button控件的点击事件,在第一个点击事件中绑定service,在第二个点击事件中通过DataService对象获取服务器端对应参数的数据。

android_service_aidl_server->MyService.java文件:

package com.example.android_service_aidl_server;

import com.example.service.DataService;

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

public class MyService extends Service {

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return binder;
}

//	定义提供给客户端(Client端)调用的方法
Binder binder=new DataService.Stub() {

@Override
public double getData(String arg) throws RemoteException {
// TODO Auto-generated method stub
if(arg.equals("a")){
return 1;
}else if(arg.equals("b")){
return 2;
}

return 0;
}
};

}
android_service_aidl_server->DataService.aidl文件:

package com.example.service;

interface DataService{
double getData(String arg);
}
android_service_aidl_client->activity_main.xml文件:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:text="绑定Service" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:text="进程间的通信" />

</RelativeLayout>
android_service_aidl_client->MainActivity.java文件:

package com.example.android_service_aidl_client;
//进程间的通信:两个apk之间需要一个通信的功能时用到
import java.sql.Connection;

import com.example.service.DataService;

import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

private Button button,button2;
private DataService dataService;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent(DataService.class.getName());
bindService(intent, connection, BIND_AUTO_CREATE);
}
});

button2.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
//					从另外一个工程中获取一个值
double result=dataService.getData("a");
System.out.println("-->"+result);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
});

}

private ServiceConnection connection=new ServiceConnection() {

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

}

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

}
};

@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;
}

}
android_service_aidl_client->DataService.aidl文件:

package com.example.service;

interface DataService{
double getData(String arg);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: