/
ServiceUtil.java
157 lines (144 loc) · 4.93 KB
/
ServiceUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.liyi.sutils.utils;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
/**
* Service 相关工具类
* <p>注:Service 是运行在主线程的,想要执行耗时操作,得在 Service 中创建个子线程</p>
*/
public class ServiceUtil {
private static final String TAG = AppUtil.class.getSimpleName();
private ServiceUtil() {
throw new UnsupportedOperationException("cannot be instantiated");
}
/**
* 判断服务是否存活
*
* @param serviceName service 的名称
* @return {@code true}: 依然存活<br>{@code false}: 已被杀死
*/
public static boolean isServiceAlive(@NonNull String serviceName) {
List<String> serviceNames = getAllRunningService();
if (serviceNames == null || serviceNames.size() == 0) return false;
for (int i = 0; i < serviceNames.size(); i++) {
if (serviceNames.get(i).equals(serviceName)) {
return true;
}
}
return false;
}
/**
* 获取所有运行的服务
*
* @return 服务名集合
*/
public static List getAllRunningService() {
ActivityManager activityManager = (ActivityManager) SUtils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> infos = activityManager.getRunningServices(0x7FFFFFFF);
List<String> names = new ArrayList<String>();
if (infos == null || infos.size() == 0) return null;
for (ActivityManager.RunningServiceInfo aInfo : infos) {
names.add(aInfo.service.getClassName());
}
return names;
}
/**
* 启动服务
*
* @param className 完整包名的服务类名
*/
public static void startService(@Nullable String className) {
try {
startService(Class.forName(className));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 启动服务
*
* @param cls 服务类
*/
public static void startService(Class<?> cls) {
Intent intent = new Intent(SUtils.getApp(), cls);
SUtils.getApp().startService(intent);
}
/**
* 停止服务
*
* @param className 完整包名的服务类名
* @return {@code true}: 停止成功<br>{@code false}: 停止失败
*/
public static boolean stopService(@Nullable String className) {
try {
return stopService(Class.forName(className));
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 停止服务
*
* @param cls 服务类
* @return {@code true}: 停止成功<br>{@code false}: 停止失败
*/
public static boolean stopService(Class<?> cls) {
Intent intent = new Intent(SUtils.getApp(), cls);
return SUtils.getApp().stopService(intent);
}
/**
* 绑定服务
*
* @param className 完整包名的服务类名
* @param conn 服务连接对象
* @param flags 绑定选项
* <ul>
* <li>{@link Context#BIND_AUTO_CREATE}</li>
* <li>{@link Context#BIND_DEBUG_UNBIND}</li>
* <li>{@link Context#BIND_NOT_FOREGROUND}</li>
* <li>{@link Context#BIND_ABOVE_CLIENT}</li>
* <li>{@link Context#BIND_ALLOW_OOM_MANAGEMENT}</li>
* <li>{@link Context#BIND_WAIVE_PRIORITY}</li>
* </ul>
*/
public static void bindService(@Nullable String className, ServiceConnection conn, int flags) {
try {
bindService(Class.forName(className), conn, flags);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 绑定服务
*
* @param cls 服务类
* @param conn 服务连接对象
* @param flags 绑定选项
* <ul>
* <li>{@link Context#BIND_AUTO_CREATE}</li>
* <li>{@link Context#BIND_DEBUG_UNBIND}</li>
* <li>{@link Context#BIND_NOT_FOREGROUND}</li>
* <li>{@link Context#BIND_ABOVE_CLIENT}</li>
* <li>{@link Context#BIND_ALLOW_OOM_MANAGEMENT}</li>
* <li>{@link Context#BIND_WAIVE_PRIORITY}</li>
* </ul>
*/
public static void bindService(Class<?> cls, ServiceConnection conn, int flags) {
Intent intent = new Intent(SUtils.getApp(), cls);
SUtils.getApp().bindService(intent, conn, flags);
}
/**
* 解绑服务
*
* @param conn 服务连接对象
*/
public static void unbindService(ServiceConnection conn) {
SUtils.getApp().unbindService(conn);
}
}