Guest User

Untitled

a guest
Mar 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | None | 0 0
  1. public class MyService extends Service
  2. {
  3. private final IMyService.Stub mBinder;
  4. private Context _mContext;
  5. private MyServiceHandler mHandler;
  6. private HanderThread mHandlerThread;
  7. private IHelperService mHelperService;
  8.  
  9. public MyService() {
  10. this.mBinder = new IMyService.Stub() {
  11. public void start_data(final String data) {
  12. try {
  13. if (MyService.this.mHelperService == null) {
  14. return;
  15. }
  16. Message msg = MyService.this.getHandler().obtainMessage(3, (Object)data);
  17. MyService.this.getHandler().sendMessage(msg);
  18. } catch (SecurityException ex) {
  19. // do some logging
  20. }
  21. };
  22. }
  23.  
  24. private void startMessageProcess(final String message) {
  25. // Process the message argument
  26. final byte[] dataArray = this.getByteArray(message);
  27. byte[] returnData;
  28. try {
  29. returnData = this.mHelperService.foo(dataArray);
  30. } catch (Exception e) {
  31. // do some logging
  32. }
  33. Message msg = this.getHandler().obtainMessage(3, (Object)message);
  34. this.getHandler.sendMessage(msg);
  35. }
  36.  
  37. private String getServerData(int id) {
  38. // builds a Uri based on id and posts it, retrieves String data from HTTP entity and returns it
  39. }
  40.  
  41. private boolean postDataToServer(final byte[] arrayData) {
  42. // builds a Uri, post data to it, gets the status code, returns true if successful
  43. }
  44.  
  45. private void sendData(final byte[] arrayData) {
  46. final Intent intent = new Intent();
  47. intent.setAction("com.my.service.intent.action.MY_RESULT");
  48. intent.putExtra("com.my.service.intent.extra.RESULT", 0);
  49. intent.putExtra("com.my.service.intent.extra.MY_DATA", arrayData);
  50. this._mContext.sendBroadcast(intent, "com.my.service.permission.MY_PERMISSION");
  51. }
  52.  
  53. public Handler getHandler() {
  54. return this.mHandler;
  55. }
  56.  
  57. public IBinder onBind(final Intent intent) {
  58. if (intent != null) {
  59. final String action = intent.getAction();
  60. if (action != null && action.equals("com.my.service.intent.action.BIND_MY_SERVICE")) {
  61. this._mContext = this.getApplicationContext();
  62. return (IBinder)this.mBinder;
  63. }
  64. }
  65. return null;
  66. }
  67.  
  68. public void onCreate() {
  69. super.onCreate();
  70. try {
  71. final Class<?> serviceManager = Class.forName("android.os.ServiceManager");
  72. this.mHelperService = mHelperService.Stub.asInterface((IBinder)serviceManager.getMethod("getService", String.class).invoke(serviceManager, "helperservice"));
  73. if (this.mHelperService == null) {
  74. return;
  75. }
  76. }
  77. catch (ClassNotFoundException ex2) {
  78. Log.e("MyService", "Helper service ClassNotFoundException !!!");
  79. }
  80. catch (Exception ex) {
  81. Log.e("MyService", "onCreate() Exception: " + Log.getStackTraceString((Throwable)ex));
  82. }
  83. this.mHandlerThread = new HandlerThread("MyService");
  84. this.mHandlerThread.start();
  85. this.mHandler = new MyServiceHandler(this.mHandlerThread.getLooper());
  86. }
  87.  
  88. public int onStartCommand(final Intent intent, final int flags, final int startId) {
  89. if (intent != null) {
  90. super.onStartCommand(intent, flags, startId);
  91. }
  92. return START_NOT_STICKY;
  93. }
  94.  
  95. private final class MyService Handler extends Handler
  96. {
  97. public MyServiceHandler(final Looper looper) {
  98. super(looper);
  99. }
  100. public void handleMessage(final Message message) {
  101. if (message != null) {
  102. switch (message.what) {
  103. case 2: {
  104. final String data = (String)message.obj;
  105. if (data != null) {
  106. MyService.this.startMessageProcess(data);
  107. return
  108. }
  109. break;
  110. }
  111. case 3: {
  112. MyService.this.sendData((byte[])message.obj);
  113. }
  114. }
  115. }
  116. }
  117. }
  118. }
  119.  
  120. public interface IMyService extends IInterface
  121. {
  122. void start_data(final String p0) throws RemoteException;
  123.  
  124. public abstract static class Stub extends Binder implements IMyService
  125. {
  126.  
  127. public Stub() {
  128. this.attachInterface((IInterface)this, "com.my.service.IMyService");
  129. }
  130.  
  131. public static IMyService asInterface(final IBinder binder) {
  132. if (binder == null) {
  133. return null;
  134. }
  135. final IInterface queryLocalInterface = binder.queryLocalInterface("com.my.service.IMyService");
  136. if (queryLocalInterface != null && queryLocalInterface instanceof IMyService) {
  137. return (IMyService)queryLocalInterface;
  138. }
  139. return new Proxy(binder);
  140. }
  141.  
  142. public IBinder asBinder() {
  143. return (IBinder)this;
  144. }
  145.  
  146. public boolean onTransact(final int code, final Parcel data, final Parcel reply, final int flags) throws RemoteException {
  147. switch (code) {
  148. case 1: {
  149. data.enforceInterface("com.my.service.IMyService");
  150. this.start_data(data.readString());
  151. reply.writeNoException();
  152. return true;
  153. }
  154. case 1000000000: {
  155. reply.writeString("com.my.service.IMyService");
  156. return true;
  157. }
  158. default: {
  159. return super.onTransact(n, data, reply, flags);
  160. }
  161. }
  162. }
  163.  
  164. private static class Proxy implements IMyService
  165. {
  166. private IBinder mRemote;
  167.  
  168. Proxy(final IBinder mRemote) {
  169. this.mRemote = mRemote;
  170. }
  171.  
  172. public IBinder asBinder() {
  173. return this.mRemote;
  174. }
  175.  
  176. public String getInterfaceDescriptor() {
  177. return "com.my.service.IMyService";
  178. }
  179.  
  180. @Override
  181. public void start_data(final String data) throws RemoteException {
  182. final Parcel data = Parcel.obtain();
  183. final Parcel reply = Parcel.obtain();
  184. try {
  185. data.writeInterfaceToken("com.my.service.IMyService");
  186. data.writeString(data);
  187. this.mRemote.transact(1, data, reply, 0);
  188. reply.readException();
  189. }
  190. finally {
  191. reply.recycle();
  192. data.recycle();
  193. }
  194. }
  195. }
  196. }
  197. }
  198.  
  199. public interface IHelperService extends IInterface
  200. {
  201. // a bunch of method declarations here
  202.  
  203. public abstract static class Stub extends Binder implements IHelperService
  204. {
  205. public Stub() {
  206. this.attachInterface((IInterface)this, "android.service.helper.IHelperService");
  207. }
  208.  
  209. public static IHelperService asInterface(final IBinder binder) {
  210. if (binder == null) {
  211. if (queryLocalInterface != null && queryLocalInterface instanceof IHelperService) {
  212. return (IHelperService)queryLocalInterface;
  213. }
  214. return new Proxy(binder);
  215. }
  216. }
  217.  
  218. public IBinder asBinder() {
  219. return (IBinder)this;
  220. }
  221.  
  222. public boolean onTransact(int code, final Parcel data, final Parcle reply, int flags) throws RemoteException {
  223. switch (code) {
  224. // does a bunch of bunch of stuff here based on the incoming code
  225. }
  226. }
  227. }
  228.  
  229. private static class Proxy implements IHelperService
  230. {
  231. private IBinder mRemote;
  232.  
  233. Proxy(final IBinder mRemote) {
  234. this.mRemote = mRemote;
  235. }
  236.  
  237. @Override
  238. // a bunch of method definitions here, everything that was declared above
  239. }
  240. }
Add Comment
Please, Sign In to add comment