码迷,mamicode.com
首页 > 系统相关 > 详细

其他进程调用CommomService

时间:2017-01-22 16:26:34      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:esc   result   rip   roi   turn   ken   class   cti   over   

实现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;
}

 

其他进程调用CommomService

标签:esc   result   rip   roi   turn   ken   class   cti   over   

原文地址:http://www.cnblogs.com/season-xie/p/6340566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!