Binder根基利用

来源:互联网 浏览:- 2020-03-31 06:10:44

Android开辟中,Binder是一种跨过程通信体例,而利用AIDL可以实现Binder的事情。

若何利用它是体味它的第一步,本文章首要记录利用Binder的一些步调。(代码思路参考《Android开辟艺术摸索》任玉刚 著)

1.建立两个activity

两个activity(OneActivity、TwoActivity),将OneActivity假定为办事端,TwoActivity假定为客户端,别离运行在不合过程中

在AndroidManifest.xml中,为TwoActivity设置过程,如许两个activity就别离运行在不合的过程中了

2. 建立AIDL文件

在AIDL文件中声明客户端想要调用办事端的别例

interface IInfManager {    /**     * Demonstrates some basic types that you can use as parameters     * and return values in AIDL.     */    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文件。

3.建立Service

Service中建立Binder工具,在onBind体例中前往这个工具,Binder工具中详细实现了IInfManager接口中的体例。Service需求在AndroidManifest.xml中注册。

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

4.办事端OneActivity

OneActivity中设置按钮跳转至TwoActivity,这里为了简朴,利用startService可以为InfManageService中name变量初始化"zhangsan"的值。也能够与客户端TwoActivity中一样,绑定service,建立连接,来设置name的值(详细参考下一步客户端的用法)。

@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_one);     Intent intent = new Intent(OneActivity.this, InfManageService.class);    intent.putExtra("name", "zhangsan");    startService(intent);     btn_one_gototwo = (Button) findViewById(R.id.btn_one_gototwo);     btn_one_gototwo.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            Intent intent = new Intent(OneActivity.this, TwoActivity.class);            startActivity(intent);        }    });}

5.客户端TwoActivity

起首绑定InfManageService办事,建立连接,连接成功后经由过程前往的IBinder工具可以获得IInfManager接口,可以经由过程这个接口去利用办事端的别例。

private TextView tv_two_name;private Button btn_two_change; private IInfManager iInfManager; private ServiceConnection connection = new ServiceConnection() {    @Override    public void onServiceConnected(ComponentName name, IBinder service) {        iInfManager = IInfManager.Stub.asInterface(service);        try {            tv_two_name.setText(iInfManager.getName());            Log.i("TwoActivity","first:" + iInfManager.getName());            iInfManager.setName("lisi");            Log.i("TwoActivity","next:" + iInfManager.getName());        }catch (RemoteException e){         }    }     @Override    public void onServiceDisconnected(ComponentName name) {     }}; @Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_two);     tv_two_name = (TextView) findViewById(R.id.tv_two_name);    btn_two_change = (Button) findViewById(R.id.btn_two_change);     Intent intent = new Intent(TwoActivity.this, InfManageService.class);    bindService(intent, connection, Context.BIND_AUTO_CREATE);     btn_two_change.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View v) {            try {                iInfManager.setName("wangmazi");                tv_two_name.setText(iInfManager.getName());            } catch (RemoteException e) {                e.printStackTrace();            }        }    });} @Overrideprotected void onDestroy() {    super.onDestroy();    unbindService(connection);}

下面代码onServiceConnected体例中,起首在TwoActivity界面中显现了办事端的name变量内容"zhangsan"

Binder基本使用

保举浏览:广东品牌网

<small id='ej'><blink></blink></small><strong id='kRYVmku'><label></label></strong><strong id='TwBvXJZB'><dfn></dfn></strong>
    <base id='JjVOOEj'><label></label></base><abbr id='GZVspWmW'><dir></dir></abbr>
      <dir id='tMWv'><strong></strong></dir><code id='mZNKGrtG'><option></option></code><legend id='UrWDGiDS'><kbd></kbd></legend>
      <label id='PCeRAoXS'><sub></sub></label><abbr id='Fv'><ins></ins></abbr><blockquote id='MOCwEWXI'><thead></thead></blockquote>
      <small id='qVNBpNp'><address></address></small>