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();
      }

    }

  • 相关阅读:
    pointer-net
    pytorch0.4版的CNN对minist分类
    python+selenium遍历某一个标签中的内容
    selenium提取不了标签文本
    IndentationError: unindent does not match any outer indentation level
    windows下载安装mysql
    引用opencv异常
    jenkins简单安装及配置(Windows环境)
    多台服务器-SSH免密登录设置
    scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别
  • 原文地址:https://www.cnblogs.com/jiww/p/5604114.html
Copyright © 2011-2022 走看看