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

AIDL初探(本地、进程)

2015-11-02 16:54 567 查看
项目地址。。。。。。。。。。。。

进程通讯方式有广播、内容提供者、AIDL

这篇博客是AIDL初探

定义AIDL接口
1、AIDL接口文件,和普通的接口内容没有什么特别,只是它的扩展名为.aidl。

2、保存在src目录下。如果其他应用程序需要IPC,则那些应用程序的src也要带有这个文件。

3、Android SDK tools就会在gen目录自动生成一个IBinder接口文件。

4、service必须适当地实现这个IBinder接口。

5、那么客户端程序就能绑定这个service并在IPC时从IBinder调用方法。

第一步 创建.aidl格式的接口文件,里面一个方法是2个数相加求和

编译你的aidl文件,这个只要是在eclipse中开发,你的adt插件会像资源文件一样把aidl文件编译成java代码生成在gen文件夹下,不用手动去编译

IAdditionService.aidl

package com.android.hellosumaidl;

interface IAdditionService {
int add(in int value1, in int value2);
}


第二步创建服务

gen文件中生成 public static abstract class Stub extends android.os.Binder implements com.android.hellosumaidl.IAdditionService

原来Stub类就是继承于Binder类,只是所返回的IBinder对象比较特别,是一个实现了AIDL接口的Binder



AdditionService

package com.android.hellosumaidl;

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

/**
* 1、AIDL接口文件,和普通的接口内容没有什么特别,只是它的扩展名为.aidl。
* 2、保存在src目录下。如果其他应用程序需要IPC, 则那些应用程序的src也要带有这个文件。
*
* 3、Android SDK tools就会在gen目录自动生成一个IBinder接口文件
* 4、service必须适当地实现这个IBinder接口
* 5、那么客户端程序就能绑定这个service并在IPC时从IBinder调用方法。
*
*/
public class AdditionService extends Service {
@Override
public void onCreate() {
super.onCreate();
}

@Override
public IBinder onBind(Intent intent) {
return new IAdditionService.Stub() {
@Override
public int add(int value1, int value2) throws RemoteException {
return value1 + value2;
}
};
}

@Override
public void onDestroy() {
super.onDestroy();
}
}


第三步 连接并绑定服务

HelloSumAidlActivity

package com.android.hellosumaidl;

import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;

public class HelloSumAidlActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

initService();

Button buttonCalc = (Button) findViewById(R.id.buttonCalc);

buttonCalc.setOnClickListener(new OnClickListener() {
TextView result = (TextView) findViewById(R.id.result);
EditText value1 = (EditText) findViewById(R.id.value1);
EditText value2 = (EditText) findViewById(R.id.value2);

@Override
public void onClick(View v) {
int v1, v2, res = -1;
v1 = Integer.parseInt(value1.getText().toString());
v2 = Integer.parseInt(value2.getText().toString());

try {
res = service.add(v1, v2);
} catch (RemoteException e) {
e.printStackTrace();
}

result.setText(Integer.valueOf(res).toString());
}
});
}

/*
* 创建ServiceConnection对象--绑定服务必须使activity与服务相关联
*/
AdditionServiceConnection connection;

private void initService() {
connection = new AdditionServiceConnection();
Intent i = new Intent(this, AdditionService.class);
//绑定服务
bindService(i, connection, Context.BIND_AUTO_CREATE);
}

/*
* This inner class is used to connect to the service
*/
IAdditionService service;

class AdditionServiceConnection implements ServiceConnection {
public void onServiceConnected(ComponentName name, IBinder boundService) {
service = IAdditionService.Stub.asInterface((IBinder) boundService);
Toast.makeText(HelloSumAidlActivity.this, "Service connected",
Toast.LENGTH_LONG).show();
}

public void onServiceDisconnected(ComponentName name) {
service = null;
Toast.makeText(HelloSumAidlActivity.this, "Service disconnected",
Toast.LENGTH_LONG).show();
Log.e("TAG", "Service disconnected");
}
}

/**
* 解绑服务,断开连接
*/
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
connection = null;
}
}


接下来看配置文件

<service
android:name="com.android.hellosumaidl.AdditionService"
android:enabled="true"
android:label="@string/app_name"
android:exported="false" >
<intent-filter>
<intent-filter>
<action android:name="com.android.hellosumaidl.IAdditionService" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</intent-filter>
</service>


最后看下资源文件



main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textSize="22sp" />

<EditText
android:id="@+id/value1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/hint1" >
</EditText>

<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/plus"
android:textSize="36sp" />

<EditText
android:id="@+id/value2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/hint2" >
</EditText>

<Button
android:id="@+id/buttonCalc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/equal" >
</Button>

<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/result"
android:textSize="36sp" />

</LinearLayout>


++++++++++++++++++++++++++++++++++++++++++两个APK进行通讯++++++++++++++++++++++++++++++++++++++++++++

先看服务端



IPerson.aidl--里面有一个方法

package com.scott.aidl;
interface IPerson {
String greet(String someone);
}


AIDLService

package com.example.testaildserver;

import com.scott.aidl.IPerson;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class AIDLService extends Service {

private static final String TAG = "AIDLService";

IPerson.Stub stub = new IPerson.Stub() {
@Override
public String greet(String someone) throws RemoteException {
Log.i(TAG, "greet() called");
return "hello, " + someone;
}
};

@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind() called");
return stub;
}

@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG, "onUnbind() called");
return true;
}

@Override
public void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy() called");
}
}


<service android:name=".AIDLService" >
<intent-filter>
<action android:name="android.intent.action.AIDLService" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>


接下来看客户端



MainActivity

package com.example.testaildclient;

import com.scott.aidl.IPerson;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

private Button bindBtn;
private Button greetBtn;
private Button unbindBtn;

private IPerson person;
private ServiceConnection conn = new ServiceConnection() {

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("ServiceConnection", "onServiceConnected() called");
person = IPerson.Stub.asInterface(service);
}

@Override
public void onServiceDisconnected(ComponentName name) {
Log.i("ServiceConnection", "onServiceDisconnected() called");
}
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

bindBtn = (Button) findViewById(R.id.bindBtn);
bindBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("android.intent.action.AIDLService");
bindService(intent, conn, Context.BIND_AUTO_CREATE);

bindBtn.setEnabled(false);
greetBtn.setEnabled(true);
unbindBtn.setEnabled(true);
}
});

greetBtn = (Button) findViewById(R.id.greetBtn);
greetBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String retVal = person.greet("scott");
Toast.makeText(MainActivity.this, retVal,
Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
Toast.makeText(MainActivity.this, "error",
Toast.LENGTH_SHORT).show();
}
}
});

unbindBtn = (Button) findViewById(R.id.unbindBtn);
unbindBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
unbindService(conn);

bindBtn.setEnabled(true);
greetBtn.setEnabled(false);
unbindBtn.setEnabled(false);
}
});
}
}


布局如下



点击bindService输出log如下

onServiceConnected() called

onBind() called

点击greet输入log如下

greet() called

点击UnbindService输入log如下

onUnbind() called

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