geo*_*eok 26
public static boolean isServiceRunningInForeground(Context context, Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
if (service.foreground) {
return true;
}
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
pio*_*rpo 23
private boolean isServiceRunning(String serviceName){
boolean serviceRunning = false;
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> l = am.getRunningServices(50);
Iterator<ActivityManager.RunningServiceInfo> i = l.iterator();
while (i.hasNext()) {
ActivityManager.RunningServiceInfo runningServiceInfo = i
.next();
if(runningServiceInfo.service.getClassName().equals(serviceName)){
serviceRunning = true;
if(runningServiceInfo.foreground)
{
//service run in foreground
}
}
}
return serviceRunning;
}
Run Code Online (Sandbox Code Playgroud)
如果您想知道您的服务是否在前台运行,只需打开其他胖应用程序,然后检查服务是否仍在运行或只是检查标志service.foreground.
从 API 26 开始,getRunningService()已弃用。
现在的一种解决方案是将您的活动绑定到您的服务。然后,您可以从活动中调用服务的方法,以检查它是否正在运行。
1 - 在您的 Service 中,创建一个扩展 Binder 并返回您的 Service 的类
public class LocalBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
Run Code Online (Sandbox Code Playgroud)
2 - 在您的服务中,声明活页夹
private final IBinder binder = new LocalBinder();
Run Code Online (Sandbox Code Playgroud)
3 - 在您的服务中,实施onBind(),这将返回 Binder
@Nullable
@Override
public IBinder onBind(Intent intent) {
return binder;
}
Run Code Online (Sandbox Code Playgroud)
4 - 在您的服务中,创建一个方法来检查它是否正在运行(例如检查变量是否已初始化)
public boolean isRunning() {
// If running
return true;
// If not running
return false;
}
Run Code Online (Sandbox Code Playgroud)
5 - 在您的活动中,创建一个保存您的服务的变量
private MyService myService;
Run Code Online (Sandbox Code Playgroud)
6 - 现在,在您的活动中,您可以绑定到您的服务
private void checkIfEnabled() {
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
MyService.LocalBinder binder = (MyService.LocalBinder) service;
myService = binder.getService();
// Calling your service public method
if(myService.isRunning()) {
// Your service is enabled
} else {
// Your service is disabled
}
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
}
};
// Bind to MyService
Intent intent = new Intent(this, MyService.class);
bindService(intent, connection, Context.BIND_AUTO_CREATE);
}
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请查看官方文档中的绑定服务概述。
答案的更有效变体:https : //stackoverflow.com/a/36127260/1275265
public static boolean isServiceRunningInForeground(Context context, Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return service.foreground;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15640 次 |
| 最近记录: |