如何从 adb shell 启动和停止 android 服务?

发布于 2024-12-04 10:07:17 字数 41 浏览 0 评论 0原文

我需要编写一个 shell 脚本来启动和停止 android 服务。

I need to write a shell script to start and stop an android service .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

埋情葬爱 2024-12-11 10:07:17

我是 Android 的初学者,但它是这样工作的:

在 AndroidManifest.xml 中,确保在 内有类似这样的内容:

<service android:name="com.some.package.name.YourServiceSubClassName" android:permission="com.some.package.name.YourServiceSubClassName">
    <intent-filter>
        <action android:name="com.some.package.name.YourServiceSubClassName"/>
    </intent-filter>
</service>

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 TimMrRoy

adb shell am startservice com.some.package.name/.YourServiceSubClassName

有趣的是,我不需要-n

为了停止,我使用

adb shell am force-stop com.some.package.name

希望它有帮助。

由于我是初学者,请随意编辑/评论以修复任何误解(例如,可能与组件(?)名称中的 .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:

<service android:name="com.some.package.name.YourServiceSubClassName" android:permission="com.some.package.name.YourServiceSubClassName">
    <intent-filter>
        <action android:name="com.some.package.name.YourServiceSubClassName"/>
    </intent-filter>
</service>

where YourServiceSubClassName extend android.app.Service is your java class that is the service. Where com.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 caused ClassNotFoundException that I'm yet to solve.

Then, install your apk. I did from eclipse but also adb install -r yourApkHere.apk should work. Uninstall is adb uninstall com.some.package.name, btw.

You can start it from host system like this, thanks Just a Tim and MrRoy:

adb shell am startservice com.some.package.name/.YourServiceSubClassName

interestingly, I didn't need -n.

To stop, I use

adb shell am force-stop com.some.package.name

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).

挽梦忆笙歌 2024-12-11 10:07:17

启动服务:

adb shell am startservice ...

启动服务。选项有:
--用户| current:指定以哪个用户身份运行;如果不
指定然后以当前用户身份运行。

停止服务:

adb shell am stopservice ... 

停止服务。选项有:
--用户| current:指定以哪个用户身份运行;如果不
指定然后以当前用户身份运行。

Starting a service:

adb shell am startservice ...

start a Service. Options are:
--user | current: Specify which user to run as; if not
specified then run as the current user.

Stopping a service:

adb shell am stopservice ... 

stop a Service. Options are:
--user | current: Specify which user to run as; if not
specified then run as the current user.

百善笑为先 2024-12-11 10:07:17

在 Oreo (26+) 中使用时,您可能会收到错误“*错误:应用程序在后台 *”

adb shell am startservice 

。这需要前台服务。
使用以下内容。

adb shell am start-foreground-service com.some.package.name/.YourServiceSubClassName

You may get an error "*Error: app is in background *" while using

adb shell am startservice 

in Oreo (26+). This requires services in the foreground.
Use the following.

adb shell am start-foreground-service com.some.package.name/.YourServiceSubClassName
白首有我共你 2024-12-11 10:07:17

如果您想在 adb shell 中运行脚本,那么我正在尝试执行相同的操作,但使用应用程序。我认为你可以使用“am start”命令

用法:am [子命令] [选项]

start an Activity: am start [-D] [-W] <INTENT>
    -D: enable debugging
    -W: wait for launch to complete

**start a Service: am startservice <INTENT>**

send a broadcast Intent: am broadcast <INTENT>

start an Instrumentation: am instrument [flags] <COMPONENT>
    -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT)
    -e <NAME> <VALUE>: set argument <NAME> to <VALUE>
    -p <FILE>: write profiling data to <FILE>
    -w: wait for instrumentation to finish before returning

start profiling: am profile <PROCESS> start <FILE>
stop profiling: am profile <PROCESS> stop

start monitoring: am monitor [--gdb <port>]
    --gdb: start gdbserv on the given port at crash/ANR

<INTENT> specifications include these flags:
    [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
    [-c <CATEGORY> [-c <CATEGORY>] ...]
    [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
    [--esn <EXTRA_KEY> ...]
    [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
    [-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
    [-n <COMPONENT>] [-f <FLAGS>]
    [--grant-read-uri-permission] [--grant-write-uri-permission]
    [--debug-log-resolution]
    [--activity-brought-to-front] [--activity-clear-top]
    [--activity-clear-when-task-reset] [--activity-exclude-from-recents]
    [--activity-launched-from-history] [--activity-multiple-task]
    [--activity-no-animation] [--activity-no-history]
    [--activity-no-user-action] [--activity-previous-is-top]
    [--activity-reorder-to-front] [--activity-reset-task-if-needed]
    [--activity-single-top]
    [--receiver-registered-only] [--receiver-replace-pending]
    [<URI>]

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]

start an Activity: am start [-D] [-W] <INTENT>
    -D: enable debugging
    -W: wait for launch to complete

**start a Service: am startservice <INTENT>**

send a broadcast Intent: am broadcast <INTENT>

start an Instrumentation: am instrument [flags] <COMPONENT>
    -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT)
    -e <NAME> <VALUE>: set argument <NAME> to <VALUE>
    -p <FILE>: write profiling data to <FILE>
    -w: wait for instrumentation to finish before returning

start profiling: am profile <PROCESS> start <FILE>
stop profiling: am profile <PROCESS> stop

start monitoring: am monitor [--gdb <port>]
    --gdb: start gdbserv on the given port at crash/ANR

<INTENT> specifications include these flags:
    [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
    [-c <CATEGORY> [-c <CATEGORY>] ...]
    [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
    [--esn <EXTRA_KEY> ...]
    [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
    [-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
    [-n <COMPONENT>] [-f <FLAGS>]
    [--grant-read-uri-permission] [--grant-write-uri-permission]
    [--debug-log-resolution]
    [--activity-brought-to-front] [--activity-clear-top]
    [--activity-clear-when-task-reset] [--activity-exclude-from-recents]
    [--activity-launched-from-history] [--activity-multiple-task]
    [--activity-no-animation] [--activity-no-history]
    [--activity-no-user-action] [--activity-previous-is-top]
    [--activity-reorder-to-front] [--activity-reset-task-if-needed]
    [--activity-single-top]
    [--receiver-registered-only] [--receiver-replace-pending]
    [<URI>]
鹿! 2024-12-11 10:07:17

我可以启动服务

am startservice com.xxx/.service.XXXService

,但我还不知道如何停止它。

I can start service through

am startservice com.xxx/.service.XXXService

but i don't know how to stop it yet.

星軌x 2024-12-11 10:07:17

要停止服务,您必须使用以下方式查找服务名称:

adb shell dumpsys activity services <your package>

例如:
adb shell dumpsys Activity services com.xyz.something

这将列出为您的包运行的服务。
输出应类似于:

ServiceRecord{xxxxx u0 com.xyz.something.beta/xyz.something.abc.XYZService}

现在选择您的服务并运行:

adb shell am stopservice <service_name> 

例如:

adb shell am stopservice com.xyz.something.beta/xyz.something.abc.XYZService

类似地,要启动服务:

adb shell am startservice <service_name>

要访问服务,您的服务(在 AndroidManifest.xml 中)应设置导出=“true”

<!-- Service declared in manifest -->
<service
    android:name=".YourServiceName"
    android:exported="true"
    android:launchMode="singleTop">
    <intent-filter>
        <action android:name="com.your.package.name.YourServiceName"/>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</service>

To stop a service, you have to find service name using:

adb shell dumpsys activity services <your package>

for example:
adb shell dumpsys activity services com.xyz.something

This will list services running for your package.
Output should be similar to:

ServiceRecord{xxxxx u0 com.xyz.something.beta/xyz.something.abc.XYZService}

Now select your service and run:

adb shell am stopservice <service_name> 

For example:

adb shell am stopservice com.xyz.something.beta/xyz.something.abc.XYZService

similarly, to start service:

adb shell am startservice <service_name>

To access service, your service(in AndroidManifest.xml) should set exported="true"

<!-- Service declared in manifest -->
<service
    android:name=".YourServiceName"
    android:exported="true"
    android:launchMode="singleTop">
    <intent-filter>
        <action android:name="com.your.package.name.YourServiceName"/>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</service>
万人眼中万个我 2024-12-11 10:07:17

您需要添加 android:exported="true" 才能从 ADB 命令行启动服务。然后你的清单看起来像这样:

<!-- Service declared in manifest -->
<service
    android:name=".YourServiceName"
    android:exported="true"
    android:launchMode="singleTop">
    <intent-filter>
        <action android:name="com.your.package.name.YourServiceName"/>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</service> <!-- Note: Service is exported to start it using ADB command -->

然后从 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:

<!-- Service declared in manifest -->
<service
    android:name=".YourServiceName"
    android:exported="true"
    android:launchMode="singleTop">
    <intent-filter>
        <action android:name="com.your.package.name.YourServiceName"/>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</service> <!-- Note: Service is exported to start it using ADB command -->

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

木有鱼丸 2024-12-11 10:07:17

您应该将服务的 android:exported 属性设置为“true”,以允许其他组件调用它。在 AndroidManifest.xml 文件中,添加以下属性:

<service android:exported="true" ></service>

然后,您应该能够通过 adb 启动服务:

adb shell am startservice com.package.name/.YourServiceName

有关 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:

<service android:exported="true" ></service>

Then, you should be able to start the service via adb:

adb shell am startservice com.package.name/.YourServiceName

For more info about the android:exported attribute see this page.

浪漫之都 2024-12-11 10:07:17

响应 pzulw 对 sandroid 关于指定意图的反馈。

组件名称的格式在 api 文档中描述为 ComponentName.unflattenFromString

它在第一个“/”处分割字符串,将前面的部分作为包名,后面的部分作为类名。为了特别方便(例如,在命令行上解析组件名称时使用),如果“/”后面紧跟着“.”。那么最终的类名将是包名与“/”后面的字符串的串联。因此,“com.foo/.Blah”变为 package="com.foo" class="com.foo.Blah"。

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

It splits the string at the first '/', taking the part before as the package name and the part after as the class name. As a special convenience (to use, for example, when parsing component names on the command line), if the '/' is immediately followed by a '.' then the final class name will be the concatenation of the package name with the string following the '/'. Thus "com.foo/.Blah" becomes package="com.foo" class="com.foo.Blah".

萌面超妹 2024-12-11 10:07:17

am startservice <意图>   

或者实际上从操作系统 shell

adb shell am startservice

am startservice <INTENT>   

or actually from the OS shell

adb shell am startservice <INTENT>

情绪 2024-12-11 10:07:17

对于仍然对如何定义服务名称参数感到困惑的人,正斜杠紧跟在完全限定类名中的应用程序包名称之后。

因此,给定应用程序包名称: app.package.name

以及服务的完整路径: app.package.name.example.package.path.MyServiceClass

然后该命令如下所示:

adb shell am startservice 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:

adb shell am startservice app.package.name/.example.package.path.MyServiceClass
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文