zoukankan      html  css  js  c++  java
  • RPC的简单实例

    先新建一个aidl文件

    IPerson.aidl

    package comhoperun.aidl;
    interface IPerson{
    void setName(String name);
    void setAge(int age);
    String display();

    }

    再新建对应的java文件

    IPersonImpl.java

    package com.hoprun.activity;

    import android.os.IBinder;
    import android.os.RemoteException;

    import comhoperun.aidl.IPerson;

    public class IPersonImpl extends IPerson.Stub {

    String name;
    int age;
    @Override
    public IBinder asBinder() {

    return null;
    }

    @Override
    public void setName(String name) throws RemoteException {
    this.name = name;

    }

    @Override
    public void setAge(int age) throws RemoteException {

    this.age = age;
    }

    @Override
    public String display() throws RemoteException {
    // TODO Auto-generated method stub
    return "name : " + name + " age : " + age;
    }

    }

    RpcService.java

    package com.hoprun.activity;
    import comhoperun.aidl.IPerson.Stub;
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;

    public class RpcService extends Service {

    //把实现类赋值给Stup,接口的静态内部类
    Stub person = new IPersonImpl();
    @Override
    public IBinder onBind(Intent intent) {
    //RPC:远程进程访问,在IPerson类中有定义
    return person;
    }

    }

    RPCServiceActivity.java

    package com.hoprun.activity;

    import android.app.Activity;
    import android.app.Service;
    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;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;

    import comhoperun.aidl.IPerson;

    public class RPCServiceActivity extends Activity {
    /** Called when the activity is first created. */
    public static final String SERVICE_STR = "com.hoperun.service";
    Button button_service;
    IPerson iperson;
    //绑定service时,调用的ServiceConnection
    ServiceConnection conn = new ServiceConnection(){

    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
    //获得接口IPerson
    iperson = IPerson.Stub.asInterface(service);
    if(iperson != null){
    try {
    //RPC方法调用
    iperson.setName("zhangsan");
    iperson.setAge(23);
    String msg = iperson.display();
    Toast.makeText(RPCServiceActivity.this, msg, Toast.LENGTH_SHORT).show();
    } catch (RemoteException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    }

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

    }

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

    //定义按钮
    button_service = (Button) findViewById(R.id.button_service);
    button_service.setText("aidl绑定Service");
    //绑定监听器
    button_service.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
    Intent intent = new Intent();
    intent.setAction(SERVICE_STR);
    //绑定service
    bindService(intent, conn, Service.BIND_AUTO_CREATE);
    }
    });
    }
    }

  • 相关阅读:
    Java实现串口通信的小样例
    poi读写Excel
    java的list类
    java集合类
    常用正则表达式
    正则表达式
    Java获取路径中的文件名(正则表达式)
    poi读取word2003(.doc文档)中的表格
    使用poi读取word2007(.docx)中的复杂表格
    使用Maven搭建SpringMVC
  • 原文地址:https://www.cnblogs.com/xingmeng/p/2455388.html
Copyright © 2011-2022 走看看