如何从 adb shell 启动和停止 android 服务?
我需要编写一个 shell 脚本来启动和停止 android 服务。
I need to write a shell script to start and stop an android service .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我是 Android 的初学者,但它是这样工作的:
在 AndroidManifest.xml 中,确保在
内有类似这样的内容:where
YourServiceSubClassName
扩展android.app.Service
是你的java类,它是服务。其中 com.some.package 是包名称,对于我来说在 AndroidManifest.xml 和 Java 中都是如此。使用一篇javabeat.net文章作为帮助,看看对于
另请注意,据说在包名称和类名称之间应该有
.service.
在文本中,我想这是一些约定,但是对于我这引起了我尚未解决的ClassNotFoundException
。然后,安装你的apk。我是从 eclipse 做的,但
adb install -r yourApkHere.apk
应该可以工作。顺便说一句,卸载是adb uninstall com.some.package.name
。您可以像这样从主机系统启动它,谢谢Just a Tim和MrRoy:
有趣的是,我不需要
-n
。为了停止,我使用
希望它有帮助。
由于我是初学者,请随意编辑/评论以修复任何误解(例如,可能与组件(?)名称中的
.service.
有关)。I'm a beginner in Android, but got it working like this:
in AndroidManifest.xml, make sure you, inside
<application>
, have something like this:where
YourServiceSubClassName
extendandroid.app.Service
is your java class that is the service. Wherecom.some.package
is the package name, for me both in AndroidManifest.xml and in Java.Used a javabeat.net article as help, look for
<service>
Note also, supposedly between the package name and the class name there should be
.service.
in the text, I guess this is some convention, but for me this causedClassNotFoundException
that I'm yet to solve.Then, install your apk. I did from eclipse but also
adb install -r yourApkHere.apk
should work. Uninstall isadb uninstall com.some.package.name
, btw.You can start it from host system like this, thanks Just a Tim and MrRoy:
interestingly, I didn't need
-n
.To stop, I use
Hope it helps.
As I'm a beginner, please feel freet to edit/comment to fix any misconceptions (eg. probably regarding
.service.
in the component (?) name).启动服务:
停止服务:
Starting a service:
Stopping a service:
在 Oreo (26+) 中使用时,您可能会收到错误“*错误:应用程序在后台 *”
。这需要前台服务。
使用以下内容。
You may get an error "*Error: app is in background *" while using
in Oreo (26+). This requires services in the foreground.
Use the following.
如果您想在 adb shell 中运行脚本,那么我正在尝试执行相同的操作,但使用应用程序。我认为你可以使用“am start”命令
用法:am [子命令] [选项]
If you want to run the script in adb shell, then I am trying to do the same, but with an application. I think you can use "am start" command
usage: am [subcommand] [options]
我可以启动服务
,但我还不知道如何停止它。
I can start service through
but i don't know how to stop it yet.
要停止服务,您必须使用以下方式查找服务名称:
例如:
adb shell dumpsys Activity services com.xyz.something
这将列出为您的包运行的服务。
输出应类似于:
现在选择您的服务并运行:
例如:
类似地,要启动服务:
要访问服务,您的服务(在 AndroidManifest.xml 中)应设置导出=“true”
To stop a service, you have to find service name using:
for example:
adb shell dumpsys activity services com.xyz.something
This will list services running for your package.
Output should be similar to:
Now select your service and run:
For example:
similarly, to start service:
To access service, your service(in AndroidManifest.xml) should set exported="true"
您需要添加
android:exported="true"
才能从 ADB 命令行启动服务。然后你的清单看起来像这样:然后从 ADB
启动服务:
adb shell am startservice com.your.package.name/.YourServiceName
要停止服务(在 Marshmallow 上):
adb shell am stopservice com.your.package.name/.YourServiceName
要停止服务(在 Jelly Bean 上):
adb shell am force-stop com.your.package.name
You need to add
android:exported="true"
to start service from ADB command line. Then your manifest looks something like this:And then from ADB
To start service:
adb shell am startservice com.your.package.name/.YourServiceName
To stop service (on Marshmallow):
adb shell am stopservice com.your.package.name/.YourServiceName
To stop service (on Jelly Bean):
adb shell am force-stop com.your.package.name
您应该将服务的 android:exported 属性设置为“true”,以允许其他组件调用它。在 AndroidManifest.xml 文件中,添加以下属性:
然后,您应该能够通过 adb 启动服务:
有关 android:exported 属性的更多信息,请参阅 此页面。
You should set the android:exported attribute of the service to "true", in order to allow other components to invoke it. In the AndroidManifest.xml file, add the following attribute:
Then, you should be able to start the service via adb:
For more info about the android:exported attribute see this page.
响应 pzulw 对 sandroid 关于指定意图的反馈。
组件名称的格式在 api 文档中描述为 ComponentName.unflattenFromString
Responding to pzulw's feedback to sandroid about specifying the intent.
The format of the component name is described in the api docs for ComponentName.unflattenFromString
am startservice <意图>
或者实际上从操作系统 shell
adb shell am startservice
am startservice <INTENT>
or actually from the OS shell
adb shell am startservice <INTENT>
对于仍然对如何定义服务名称参数感到困惑的人,正斜杠紧跟在完全限定类名中的应用程序包名称之后。
因此,给定应用程序包名称:
app.package.name
以及服务的完整路径:
app.package.name.example.package.path.MyServiceClass
然后该命令如下所示:
For anyone still confused about how to define the service name parameter, the forward slash goes immediately after the application package name in the fully qualified class name.
So given an application package name of:
app.package.name
And a full path to the service of:
app.package.name.example.package.path.MyServiceClass
Then the command would look like this: