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

    }

  • 相关阅读:
    FastAPI数据库系列(一) MySQL数据库操作
    FastAPI响应系列(二) 响应状态码
    FastAPI系列学习目录
    FastAPI大型目录程序设计
    FastAPI异步任务系列(一) FastAPI后台任务
    FastAPI安全系列(三) 基于Hash Password和JWT Bearer Token的OAuth2 .0认证
    FastAPI安全系列(二) 基于Password和Bearer Token的OAuth2 .0认证
    FastAPI安全系列(一) OAuth2 .0认证基础
    you can run: npm install --save core-js/modules/es.regexp.exec core-js/modules/es.string.replace(已解决)
    BFC总结
  • 原文地址:https://www.cnblogs.com/jiww/p/5604114.html
Copyright © 2011-2022 走看看