zoukankan      html  css  js  c++  java
  • 使用AIDL将接口暴露给客户端(远程绑定Service)

    import java.util.Timer;
    import java.util.TimerTask;

    import jww.mediaprovidertest.ICat.Stub;
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.os.RemoteException;

    public class AidlService extends Service{
      private CatBinder catBinder;
      Timer timer = new Timer();
      String[] colors = new String[]{
        "红色","黄色","黑色"
      };
      double[] weights = new double[]{
        2.3 , 3.1 ,1.58
      };
      private String color;
      private double weight;

      //继承Stub,也就是实现了ICat接口,并实现了IBinder接口
      public class CatBinder extends Stub{

        @Override
        public String getColor() throws RemoteException {
          return color;
        }

        @Override
        public double getWeight() throws RemoteException {
          return weight;
        }

      }

      @Override
      public void onCreate() {
        super.onCreate();
        catBinder = new CatBinder();
        timer.schedule(new TimerTask() {

          @Override
          public void run() {
          // 随即地改变Service组件内color、weight属性的值
          int rand = (int)(Math.random()*3);
          color = colors[rand];
          weight = weights[rand];
          System.out.println("------"+rand);
          }
        }, 0 ,800);
      }

      @Override
      public IBinder onBind(Intent intent) {
        /*
        * 返回catBinder对象
        * 在绑定本地Service的情况下,该catBinder对象会直接
        * 传给客户端的ServiceConnection对象
        * 的onServiceConnected方法的第二个参数
        * 在绑定远程Service的情况下,只将catBinder对象的代理
        * 传给客户端的ServiceConnection对象
        * 的onServiceConnected方法的第二个参数
        */
        return catBinder;
      }

      @Override
      public void onDestroy() {
        timer.cancel();
      }

    }

  • 相关阅读:
    VB.NET导出excel并支持中文文件名 中文编码
    后台弹框。刷新不提示确认VB或.NET
    VB.NET读取保存项目中相对路径的XML
    禁止删除表里所有数据
    验证视图状态 MAC 失败的解决办法
    Flash OBJECT IIS7.0上传文件限制的解决方法
    jquery导航菜单上下都行,可以上弹也可以下拉,方便配置使用
    android开发环境之ADT安装,卸载,更新 ADT在线代理网址
    原创 C# 正则表达式 读写 Ini 文件
    原创C# 枚举 多状态 操作
  • 原文地址:https://www.cnblogs.com/jiww/p/5604114.html
Copyright © 2011-2022 走看看