ServiceUtils.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package com.cooleshow.base.utils;
  2. import android.app.ActivityManager;
  3. import android.app.ActivityManager.RunningServiceInfo;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.ServiceConnection;
  7. import android.os.Build;
  8. import java.util.HashSet;
  9. import java.util.List;
  10. import java.util.Set;
  11. import androidx.annotation.NonNull;
  12. /**
  13. * <pre>
  14. * author: Blankj
  15. * blog : http://blankj.com
  16. * time : 2016/08/02
  17. * desc : utils about service
  18. * </pre>
  19. */
  20. public final class ServiceUtils {
  21. private ServiceUtils() {
  22. throw new UnsupportedOperationException("u can't instantiate me...");
  23. }
  24. /**
  25. * Return all of the services are running.
  26. *
  27. * @return all of the services are running
  28. */
  29. public static Set<String> getAllRunningServices() {
  30. ActivityManager am = (ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
  31. List<RunningServiceInfo> info = am.getRunningServices(0x7FFFFFFF);
  32. Set<String> names = new HashSet<>();
  33. if (info == null || info.size() == 0) return null;
  34. for (RunningServiceInfo aInfo : info) {
  35. names.add(aInfo.service.getClassName());
  36. }
  37. return names;
  38. }
  39. /**
  40. * Start the service.
  41. *
  42. * @param className The name of class.
  43. */
  44. public static void startService(@NonNull final String className) {
  45. try {
  46. startService(Class.forName(className));
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. /**
  52. * Start the service.
  53. *
  54. * @param cls The service class.
  55. */
  56. public static void startService(@NonNull final Class<?> cls) {
  57. startService(new Intent(Utils.getApp(), cls));
  58. }
  59. /**
  60. * Start the service.
  61. *
  62. * @param intent The intent.
  63. */
  64. public static void startService(Intent intent) {
  65. try {
  66. intent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
  67. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  68. Utils.getApp().startForegroundService(intent);
  69. } else {
  70. Utils.getApp().startService(intent);
  71. }
  72. } catch (Exception e) {
  73. e.printStackTrace();
  74. }
  75. }
  76. /**
  77. * Stop the service.
  78. *
  79. * @param className The name of class.
  80. * @return {@code true}: success<br>{@code false}: fail
  81. */
  82. public static boolean stopService(@NonNull final String className) {
  83. try {
  84. return stopService(Class.forName(className));
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. return false;
  88. }
  89. }
  90. /**
  91. * Stop the service.
  92. *
  93. * @param cls The name of class.
  94. * @return {@code true}: success<br>{@code false}: fail
  95. */
  96. public static boolean stopService(@NonNull final Class<?> cls) {
  97. return stopService(new Intent(Utils.getApp(), cls));
  98. }
  99. /**
  100. * Stop the service.
  101. *
  102. * @param intent The intent.
  103. * @return {@code true}: success<br>{@code false}: fail
  104. */
  105. public static boolean stopService(@NonNull Intent intent) {
  106. try {
  107. return Utils.getApp().stopService(intent);
  108. } catch (Exception e) {
  109. e.printStackTrace();
  110. return false;
  111. }
  112. }
  113. /**
  114. * Bind the service.
  115. *
  116. * @param className The name of class.
  117. * @param conn The ServiceConnection object.
  118. * @param flags Operation options for the binding.
  119. * <ul>
  120. * <li>0</li>
  121. * <li>{@link Context#BIND_AUTO_CREATE}</li>
  122. * <li>{@link Context#BIND_DEBUG_UNBIND}</li>
  123. * <li>{@link Context#BIND_NOT_FOREGROUND}</li>
  124. * <li>{@link Context#BIND_ABOVE_CLIENT}</li>
  125. * <li>{@link Context#BIND_ALLOW_OOM_MANAGEMENT}</li>
  126. * <li>{@link Context#BIND_WAIVE_PRIORITY}</li>
  127. * </ul>
  128. */
  129. public static void bindService(@NonNull final String className,
  130. @NonNull final ServiceConnection conn,
  131. final int flags) {
  132. try {
  133. bindService(Class.forName(className), conn, flags);
  134. } catch (Exception e) {
  135. e.printStackTrace();
  136. }
  137. }
  138. /**
  139. * Bind the service.
  140. *
  141. * @param cls The service class.
  142. * @param conn The ServiceConnection object.
  143. * @param flags Operation options for the binding.
  144. * <ul>
  145. * <li>0</li>
  146. * <li>{@link Context#BIND_AUTO_CREATE}</li>
  147. * <li>{@link Context#BIND_DEBUG_UNBIND}</li>
  148. * <li>{@link Context#BIND_NOT_FOREGROUND}</li>
  149. * <li>{@link Context#BIND_ABOVE_CLIENT}</li>
  150. * <li>{@link Context#BIND_ALLOW_OOM_MANAGEMENT}</li>
  151. * <li>{@link Context#BIND_WAIVE_PRIORITY}</li>
  152. * </ul>
  153. */
  154. public static void bindService(@NonNull final Class<?> cls,
  155. @NonNull final ServiceConnection conn,
  156. final int flags) {
  157. bindService(new Intent(Utils.getApp(), cls), conn, flags);
  158. }
  159. /**
  160. * Bind the service.
  161. *
  162. * @param intent The intent.
  163. * @param conn The ServiceConnection object.
  164. * @param flags Operation options for the binding.
  165. * <ul>
  166. * <li>0</li>
  167. * <li>{@link Context#BIND_AUTO_CREATE}</li>
  168. * <li>{@link Context#BIND_DEBUG_UNBIND}</li>
  169. * <li>{@link Context#BIND_NOT_FOREGROUND}</li>
  170. * <li>{@link Context#BIND_ABOVE_CLIENT}</li>
  171. * <li>{@link Context#BIND_ALLOW_OOM_MANAGEMENT}</li>
  172. * <li>{@link Context#BIND_WAIVE_PRIORITY}</li>
  173. * </ul>
  174. */
  175. public static void bindService(@NonNull final Intent intent,
  176. @NonNull final ServiceConnection conn,
  177. final int flags) {
  178. try {
  179. Utils.getApp().bindService(intent, conn, flags);
  180. } catch (Exception e) {
  181. e.printStackTrace();
  182. }
  183. }
  184. /**
  185. * Unbind the service.
  186. *
  187. * @param conn The ServiceConnection object.
  188. */
  189. public static void unbindService(@NonNull final ServiceConnection conn) {
  190. Utils.getApp().unbindService(conn);
  191. }
  192. /**
  193. * Return whether service is running.
  194. *
  195. * @param cls The service class.
  196. * @return {@code true}: yes<br>{@code false}: no
  197. */
  198. public static boolean isServiceRunning(@NonNull final Class<?> cls) {
  199. return isServiceRunning(cls.getName());
  200. }
  201. /**
  202. * Return whether service is running.
  203. *
  204. * @param className The name of class.
  205. * @return {@code true}: yes<br>{@code false}: no
  206. */
  207. public static boolean isServiceRunning(@NonNull final String className) {
  208. try {
  209. ActivityManager am = (ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
  210. List<RunningServiceInfo> info = am.getRunningServices(0x7FFFFFFF);
  211. if (info == null || info.size() == 0) return false;
  212. for (RunningServiceInfo aInfo : info) {
  213. if (className.equals(aInfo.service.getClassName())) return true;
  214. }
  215. return false;
  216. } catch (Exception ignore) {
  217. return false;
  218. }
  219. }
  220. }