如何确定Android服务是否在前台运行?
我有一个我认为在前台运行的服务,如何检查我的实现是否工作?如何确定Android服务是否在前台运行?,android,service,foreground,Android,Service,Foreground,我有一个我认为在前台运行的服务,如何检查我的实现是否工作?private boolean isServiceRunning(String serviceName){ private boolean isServiceRunning(String serviceName){ boolean serviceRunning = false; ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SER
private boolean isServiceRunning(String serviceName){
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;
}
布尔serviceRunning=false;
ActivityManager am=(ActivityManager)this.getSystemService(ACTIVITY_服务);
列表l=am.getRunningServices(50);
迭代器i=l.迭代器();
while(i.hasNext()){
ActivityManager.RunningServiceInfo RunningServiceInfo=i
.next();
if(runningServiceInfo.service.getClassName().equals(serviceName)){
serviceRunning=true;
if(runningServiceInfo.前台)
{
//前台服务运行
}
}
}
返回服务运行;
}
如果您想知道您的服务是否在前台运行,只需打开一些其他fat应用程序,然后检查服务是否仍在运行,或者只需检查flagservice.foreground
公共静态布尔值IsServiceRunningForeground(上下文上下文,类serviceClass){
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;
}
ActivityManager=(ActivityManager)context.getSystemService(context.ACTIVITY_服务);
对于(ActivityManager.RunningServiceInfo服务:manager.getRunningServices(Integer.MAX_值)){
if(serviceClass.getName().equals(service.service.getClassName())){
if(服务前台){
返回true;
}
}
}
返回false;
}
更有效的答案变体:
公共静态布尔IsServiceRunningForeground(上下文上下文,类serviceClass){
ActivityManager=(ActivityManager)context.getSystemService(context.ACTIVITY_服务);
对于(ActivityManager.RunningServiceInfo服务:manager.getRunningServices(Integer.MAX_值)){
if(serviceClass.getName().equals(service.service.getClassName())){
返回服务。前台;
}
}
返回false;
}
这在加密新闻应用程序中对我有效
这是Kotlin最简洁的解决方案。多亏了这一点
@Suppress(“弃用”)//已弃用第三方服务。
有趣的上下文。isServiceRunning(服务:类)=
(getSystemService(ACTIVITY_SERVICE)作为ActivityManager)
.getRunningServices(整数.MAX_值)
.any{it.service.className==service.name}
亚当的答案很简单:
@Suppress("DEPRECATION") // Deprecated for third party Services.
fun <T> Context.isServiceForegrounded(service: Class<T>) =
(getSystemService(ACTIVITY_SERVICE) as? ActivityManager)
?.getRunningServices(Integer.MAX_VALUE)
?.find { it.service.className == service.name }
?.foreground == true
@Suppress(“弃用”)//已弃用第三方服务。
有趣的上下文。ISServiceForgrounded(服务:类)=
(getSystemService(ACTIVITY_SERVICE)作为?ActivityManager)
?.getRunningServices(整数.MAX_值)
?查找{it.service.className==service.name}
?前景==真
自API 26起,getRunningService()
已被弃用
现在的一个解决方案是将您的活动绑定到您的服务。然后,您可以从活动中调用服务的方法,以检查它是否正在运行
1-在您的服务中,创建一个扩展绑定器并返回服务的类
public class LocalBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
private MyService myService;
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);
}
2-在您的服务中,申报活页夹
private final IBinder binder = new LocalBinder();
3-在您的服务中,实现onBind()
,这将返回活页夹
@Nullable
@Override
public IBinder onBind(Intent intent) {
return binder;
}
4-在您的服务中,创建一个方法来检查它是否正在运行(例如检查变量是否已初始化)
5-在您的活动中,创建一个保存服务的变量
public class LocalBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
private MyService myService;
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);
}
6-现在,在您的活动中,您可以绑定到您的服务
public class LocalBinder extends Binder {
MyService getService() {
return MyService.this;
}
}
private MyService myService;
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);
}
有关更多信息,请查看官方文档。谢谢,效果很好!我只需要添加这一行:serviceInForeground=runningServiceInfo.前台;onDestroy()中的stopForeground(true)如何?这不会阻止服务在前台运行吗?@user811985仅供参考,RunningServiceInfo.foreground上的文档说明:“如果服务请求作为前台进程运行,则设置为true。”。也就是说,它不会告诉你android是否真的接受了前台的服务。不幸的是,GetRunningServices现在被标记为Decredit。我发现此答案中的代码比接受答案中的代码更整洁,并且与接受答案不同,即使有50多个服务在运行,它也能工作。与接受答案相同,但更清晰、更短、简单。还要检查所有服务,而不仅仅是50。这里唯一的问题是“从Oreo开始,此方法不再适用于第三方应用程序。为了向后兼容,它仍将返回调用方自己的服务。”@diesersamat那么你的建议是什么?如果我想在Android O中做同样的事情呢?如果你需要停止一个服务,这是Android 9.0的最新版本。问题是关于在前台运行的服务。因此,您还应该检查
it.foreground
。