android Binder基本使用

android Binder基本使用
一.Android 选择binder好处
采用C/S的通信模式。而在linux通信机制中,目前只有socket支持C/S的通信模式,但socket有其劣势,具体参看第二条。
有更好的传输性能。对比于Linux的通信机制,
socket:是一个通用接口,导致其传输效率低,开销大;
管道和消息队列:因为采用存储转发方式,所以至少需要拷贝2次数据,效率低;
共享内存:虽然在传输时没有拷贝数据,但其控制机制复杂(比如跨进程通信时,需获取对方进程的pid,得多种机制协同操作)。
安全性更高。Linux的IPC机制在本身的实现中,并没有安全措施,得依赖上层协议来进行安全控制。而Binder机制的UID/PID是由Binder机制本身在内核空间添加身份标识,安全性高;并且Binder可以建立私有通道,这是linux的通信机制所无法实现的(Linux访问的接入点是开放的)。
另外一个优点是对用户来说,通过binder屏蔽了client的调用server的隔阂,client端函数的名字、参数和返回值和server的方法一模一样,对用户来说犹如就在本地(也可以做得不一样),这样的体验或许其他ipc方式也可以实现,但binder出生那天就是为此而生。
二.基本使用
1.开发工具选择:
androidStudio
2.创建两个acvitity
两个activity(MainActivity、TwoMainActivity),将MainActivity假设为服务端,TwoMainActivity假设为客户端,分别运行在不同进程中

在AndroidManifest.xml中,为TwoMainActivity设置进程,这样两个activity就分别运行在不同的进程中了

3.创建InfManageService.aidl
interface IInfManager {

void setName(String name);

String getName();
}

AIDL文件声明完,activity等文件并不能调用到IInfManager接口,需要在app的build.gradle文件中的android{}中添加

sourceSets{
main{
java.srcDirs = [‘src/main/java’, ‘src/main/aidl’]
}

然后点击sync now按钮,activity文件就可以调用到IInfManager接口了,可以在app\build\generated\source\aidl\debug文件下找到自动生成的IInfManager.java文件。

4.创建InfManageService
Service中创建Binder对象,在onBind方法中返回这个对象,Binder对象中具体实现了IInfManager接口中的方法。Service需要在AndroidManifest.xml中注册。

package water.down.com.myapplication;

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

public class InfManageService extends Service {

private String name;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
name = intent.getStringExtra(“name”);
return super.onStartCommand(intent, flags, startId);
}

@Override
public IBinder onBind(Intent intent) {
return binder;
}

//最终调用此方法

private Binder binder = new IInfManager.Stub() {
@Override
public void setName(String mName) throws RemoteException {
name = mName;
}

@Override
public String getName() throws RemoteException {
return name;
}
};
}

5.服务端MainActivity
MainActivity中设置按钮跳转至TwoMainActivity,这里为了简单,使用startService可以为InfManageService中name变量初始化"zhangsan"的值。也可以与客户端TwoMainActivity中一样,绑定service,建立连接,来设置name的值(具体参考下一步客户端的用法)。

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt = (Button)findViewById(R.id.igoto);
Intent intent = new Intent(MainActivity.this, InfManageService.class);
intent.putExtra(“name”, “zhangsan”);//启动InfManageService,intent传输数据
startService(intent);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, TwoMainActivity.class);//跳转到客户端
startActivity(intent);
}
});
}

6.客户端TwoMainActivity
首先绑定InfManageService服务,建立连接,连接成功后通过返回的IBinder对象可以获得IInfManager接口,可以通过这个接口去使用服务端的方法。

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secend_activity);
tv2 = (TextView)findViewById(R.id.secend_id);
tv3 = (TextView)findViewById(R.id.third_id);

Intent intent = new Intent(TwoMainActivity.this, InfManageService.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);//InfManageService绑定ServiceConnection
tv2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
tv2.setText(infManager.getName());
}catch (RemoteException e){
finish();
}
}
});
tv3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
infManager.setName(“222”);
tv3.setText(infManager.getName());
}catch (RemoteException e){
finish();
}
}
});
}

//InfManageService 中binder回调
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
infManager = IInfManager.Stub.asInterface(service);
try {
tv2.setText(infManager.getName());
tv3.setText(infManager.getName());

Log.d(“11”,“infManager.getName()=”+infManager.getName());
}catch (RemoteException e){
finish();
}

}

@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(“1”,“onServiceDisconnected”);

}
};

@Override
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
}

7.流程图

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值