如何在android中使用adb shell知道服务是否正在运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10896629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:21:36  来源:igfitidea点击:

How to know whether service is running using adb shell in android

androidadb

提问by AndroDev

I want to know whether media player service (registers with media.player when device boots up) is running or not using adb shell. Is it possible?

我想知道媒体播放器服务(在设备启动时向 media.player 注册)是否正在使用 adb shell 运行。是否可以?

I tried running ps command but no success.

我尝试运行 ps 命令但没有成功。

回答by user276648

As mentioned already, adb shell service listwill only list system services.

如前所述,adb shell service list只会列出系统服务。

As explained in Android Emulator: How can I get a list of services that are running, you can look for services created by apps by using

Android Emulator: How can I get a list of services that are running 中所述,您可以使用以下命令查找由应用程序创建的服务

// List all services
adb shell dumpsys activity services

// List all services containing "myservice" in its name
adb shell dumpsys activity services myservice

If it returns something, it means the service is installed. To know if the service is currently started or stopped, look for app=ProcessRecord(...)or app=nullrespectively.

如果它返回某些内容,则表示该服务已安装。要了解服务当前是启动还是停止,请 分别查找app=ProcessRecord(...)app=null

You can also do it Linux style with a simple

你也可以用一个简单的 Linux 风格来做

ps | grep myservice

while inside of your shell.

而在你的外壳内。

回答by Dr. Ferrol Blackmon

Try the command line

试试命令行

adb shell service list

adb shell service list

I get a list of service names and their package names as well.

我还得到了服务名称及其包名称的列表。

回答by Paul Ratazzi

To simply check whether a specific service is running, use:

要简单地检查特定服务是否正在运行,请使用:

adb shell service check <service>

For example, adb shell service check media.playergives Service media.player: foundif it's running and Service media.player: not foundotherwise.

例如,adb shell service check media.player给出Service media.player: found它是否正在运行,Service media.player: not found否则给出。

If you need more detail, try dumpsys <service>. For example, adb shell dumpsys media.playerreturns information about media.player's clients, open files, etc.

如果您需要更多详细信息,请尝试dumpsys <service>。例如,adb shell dumpsys media.player返回有关media.player的客户端、打开的文件等的信息。

Finally, if you really need serious detail for debugging, try adb shell dumpsys activity serviceswhich shows what's going on from ActivityManager's point of view. This includes information about intents, create times, last activity time, bindings, etc., etc. You can redirect the output if you want to store it for later viewing/searching. It's typically rather lengthy.

最后,如果您确实需要用于调试的严肃细节,请尝试adb shell dumpsys activity servicesActivityManager的角度显示正在发生的事情。这包括有关意图、创建时间、上次活动时间、绑定等的信息。如果您想存储它以供以后查看/搜索,您可以重定向输出。它通常相当长。

回答by JerryGoyal

To know whether an app process is running or not (background or foreground):

要了解应用程序进程是否正在运行(后台或前台):

adb shell pidof <package.name>

It'll return empty string if process is not running else its pid.

如果进程没有运行它的pid,它将返回空字符串。