• 自动化进程调用CommomService获取androidapi功能


    应用实例:

    uiautomator1.0自动化框架因为无法调用安卓的api,所以导致很多东西无法完成,例如通过安卓api获取设备imei,连接网络,调整时间等

    在此我们可以写一个app,提供公共的service调用,并执行完返回相应的值

    下面是demo:

    实现Service方法

    public class MyPlayer extends Stub {
    
     @Override
        public void setName(String name) throws RemoteException {
    
            this.name = name;
            Log.i("magc", "setName--"+name);
        }

     

    设定一个代理service

    class MyService extends Service {
    private Stub player = new MyPlayer(); @Override public IBinder onBind(Intent arg0) { return player; }

    放入公共service中

      Class sm;
            try {
                sm = Class.forName("android.os.ServiceManager");
                Method getService = sm.getMethod("getService", new Class[] { String.class });
                getService.invoke(sm, new Object[]{"lovefang.stadyService.IPlayer", player});

    其他进程调用:

     private static IPlayer bridge;
    
      static
      {
        Class sm;
        try
        {
          sm = Class.forName("android.os.ServiceManager");
          Method getService = sm.getMethod("getService", new Class[] { String.class });
          IBinder binder = (IBinder)getService.invoke(sm, new Object[] { "lovefang.stadyService.IPlayer" });
          bridge = IPlayer.Stub.asInterface(binder);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    
      public static void todo() throws RemoteException{
          bridge.setName("test");
      }

     

    详细:

    public interface IPlayer extends android.os.IInterface {
        /** Local-side IPC implementation stub class. */
        public static abstract class Stub extends android.os.Binder implements
                IPlayer {
            private static final java.lang.String DESCRIPTOR = "lovefang.stadyService.IPlayer";
    
            /** Construct the stub at attach it to the interface. */
            public Stub() {
                this.attachInterface(this, DESCRIPTOR);
            }
    
            /**
             * Cast an IBinder object into an com.magc.rpc.IPlayer interface,
             * generating a proxy if needed.
             */
            public static IPlayer asInterface(android.os.IBinder obj) {
                if ((obj == null)) {
                    return null;
                }
                android.os.IInterface iin = (android.os.IInterface) obj
                        .queryLocalInterface(DESCRIPTOR);
                if (((iin != null) && (iin instanceof IPlayer))) {
                    return ((IPlayer) iin);
                }
                return new IPlayer.Stub.Proxy(obj);
            }
    
            public android.os.IBinder asBinder() {
                return this;
            }
    
            @Override
            public boolean onTransact(int code, android.os.Parcel data,
                    android.os.Parcel reply, int flags)
                    throws android.os.RemoteException {
                switch (code) {
                case INTERFACE_TRANSACTION: {
                    reply.writeString(DESCRIPTOR);
                    return true;
                }
                case TRANSACTION_setName: {
                    data.enforceInterface(DESCRIPTOR);
                    java.lang.String _arg0;
                    _arg0 = data.readString();
                    this.setName(_arg0);
                    reply.writeNoException();
                    return true;
                }
                case TRANSACTION_addFile: {
                    data.enforceInterface(DESCRIPTOR);
                    java.lang.String _arg0;
                    _arg0 = data.readString();
                    this.addFile(_arg0);
                    reply.writeNoException();
                    return true;
                }
                case TRANSACTION_ToString: {
                    data.enforceInterface(DESCRIPTOR);
                    java.lang.String _result = this.ToString();
                    reply.writeNoException();
                    reply.writeString(_result);
                    return true;
                }
                }
                return super.onTransact(code, data, reply, flags);
            }
    
            private static class Proxy implements IPlayer {
                private android.os.IBinder mRemote;
    
                Proxy(android.os.IBinder remote) {
                    mRemote = remote;
                }
    
                public android.os.IBinder asBinder() {
                    return mRemote;
                }
    
                public java.lang.String getInterfaceDescriptor() {
                    return DESCRIPTOR;
                }
    
                public void setName(java.lang.String name)
                        throws android.os.RemoteException {
                    android.os.Parcel _data = android.os.Parcel.obtain();
                    android.os.Parcel _reply = android.os.Parcel.obtain();
                    try {
                        _data.writeInterfaceToken(DESCRIPTOR);
                        _data.writeString(name);
                        mRemote.transact(Stub.TRANSACTION_setName, _data, _reply, 0);
                        _reply.readException();
                    } finally {
                        _reply.recycle();
                        _data.recycle();
                    }
                }
    
                public void addFile(java.lang.String f_name)
                        throws android.os.RemoteException {
                    android.os.Parcel _data = android.os.Parcel.obtain();
                    android.os.Parcel _reply = android.os.Parcel.obtain();
                    try {
                        _data.writeInterfaceToken(DESCRIPTOR);
                        _data.writeString(f_name);
                        mRemote.transact(Stub.TRANSACTION_addFile, _data, _reply, 0);
                        _reply.readException();
                    } finally {
                        _reply.recycle();
                        _data.recycle();
                    }
                }
    
                public java.lang.String ToString()
                        throws android.os.RemoteException {
                    android.os.Parcel _data = android.os.Parcel.obtain();
                    android.os.Parcel _reply = android.os.Parcel.obtain();
                    java.lang.String _result;
                    try {
                        _data.writeInterfaceToken(DESCRIPTOR);
                        mRemote.transact(Stub.TRANSACTION_ToString, _data, _reply,
                                0);
                        _reply.readException();
                        _result = _reply.readString();
                    } finally {
                        _reply.recycle();
                        _data.recycle();
                    }
                    return _result;
                }
            }
    
            static final int TRANSACTION_setName = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
            static final int TRANSACTION_addFile = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
            static final int TRANSACTION_ToString = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2);
        }
    
        public void setName(java.lang.String name)
                throws android.os.RemoteException;
    
        public void addFile(java.lang.String f_name)
                throws android.os.RemoteException;
    
        public java.lang.String ToString() throws android.os.RemoteException;
    }

     

  • 相关阅读:
    Eclipse IDE中Android项目打红叉的解决方法
    控件:PopupWindow 弹出窗口(基本操作)
    控件:AnalogClock与DigitalClock 时钟组件
    四大组件之一 BroadcastReceiver (拦截短信并屏蔽系统的Notification .)
    四大组件之一 文件存储_文本文件
    控件:Chronometer 计时器(定时震动)
    计算页面执行时间的两种方法
    URL解析的几种模式以及拟静态重定向问题
    SSH 文件上传错误:encountered 1 errors during the transfer终极解决方法:
    php过滤提交信息防注入
  • 原文地址:https://www.cnblogs.com/season-xie/p/6340566.html
Copyright © 2020-2023  润新知