gpt4 book ai didi

android - 检查服务是否正在运行

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:06:50 36 4
gpt4 key购买 nike

我有一项服务,当我启动它时,与警报管理器一起安排在 00:00 运行,我想检查我的服务是否正在运行?

当我检查“设置”->“应用程序”->“正在运行的任务”时,我的服务存在于缓存进程中,因此以下代码无法正常工作,因为我的服务在运行服务中出现,任何人都可以帮助我吗?

private boolean isServiceRunning(String serviceName) {
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 = (ActivityManager.RunningServiceInfo) i.next();
if( runningServiceInfo.service.getClassName().equals(serviceName) ) {
return true;
}
}
return false;
}

最佳答案

BindService如果您没有成功绑定(bind)到服务,则返回 false:

Returns

If you have successfully bound to the service, true is returned; false is returned if the connection is not made so you will not receive the service object.

我会用它来检查服务是否未运行。

只要确保不绑定(bind) AUTO_CREATE 标志,否则服务也会被创建。

关于android - 检查服务是否正在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9899022/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com