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

aidl

2013-12-24 17:13 99 查看
aidl:android interface definition language 安卓接口定义医院

远程服务是运行在另外一个进程里面

调用者是运行在一个进程里面

远程服务 修改:

1.把.java改为.aidl

没有public private修饰符

2.自动生成一个IService.java接口文件

3.继承IService.Stub

1.创建与之一样的包名

把aidl文件拷贝过来

2.

编译器自动生成的

不同进程可以传递数据

IPC implementation

inter process communication:进程间通讯

远程服务和本地服务方法调用的异同:

本地服务 一般定义一个 Iservice.java接口

远程服务 定义一个Iservice.aidl远程接口 修改文件扩展名 删除访问的修饰符

本地服务 开启可以采用显示意图

远程服务 开启采用隐士意图 定义action

本地服务 中间人 MyBinder extend Binder implements IService接口

远程服务 中间人 MyBinder extend (自动生成) IService.stub (IPC实现)

本地服务 获取中间人 onServiceConnected(ComponentName name,IBinder service)

吧service做一个强制类型转 IService的类型

远程服务 获取中间人 onServiceConnected(ComponentName name,IBinder service)

利用 IService.Stub.asInterface();得到远程接口的一个代理对象

远程服务

IService.aidl

package com.itheima.remoteservice;

interface IService {
void callMethodInService();
}
package com.itheima.remoteservice;

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

public class RemoteService extends Service {

@Override
public IBinder onBind(Intent intent) {
System.out.println("远程服务 onbind");
return new MyBinder();
}
//1.把接口 .java文件 扩展名改为.aidl
//2. 自动生成一个IService.java接口文件
//3. 继承 IService.Stub
private class MyBinder extends IService.Stub{
@Override
public void callMethodInService() throws RemoteException {
methodInService();
}
}

@Override
public void onCreate() {
System.out.println("远程服务 oncreate");
super.onCreate();
}

@Override
public void onDestroy() {
System.out.println("远程服务 ondestroy");
super.onDestroy();
}

@Override
public boolean onUnbind(Intent intent) {
System.out.println("远程服务 onunbind");
return super.onUnbind(intent);
}

public void methodInService(){
System.out.println("我是远程服务里面的方法....我被调用了.....");
}
}
清单文件中

<service android:name="com.itheima.remoteservice.RemoteService" >
<intent-filter>
<action android:name="com.itheima.remoteservice"/>
</intent-filter>
</service>


调用服务的工程

1.创建与之一样的包名

把aidl文件拷贝过来

package com.itheima.callremoteservice;

import com.itheima.remoteservice.IService;

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.View;

public class MainActivity extends Activity {
private Intent intent ;
private MyConn conn;
private IService iService;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent();
conn = new MyConn();
intent.setAction("com.itheima.remoteservice");
}
private class MyConn implements ServiceConnection{

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iService = IService.Stub.asInterface(service);
}

@Override
public void onServiceDisconnected(ComponentName name) {

}

}

public void start(View view) {
startService(intent);
}

public void stop(View view) {
stopService(intent);
}

public void bind(View view) {
bindService(intent, conn, BIND_AUTO_CREATE);
}

public void unbind(View view) {

}

public void call(View view) {
try {
iService.callMethodInService();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}


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