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

时间:2023-01-26 20:45:51

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

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

10 个解决方案

#1


34  

I'm a beginner in Android, but got it working like this:

我是Android的初学者,但它的工作原理如下:

in AndroidManifest.xml, make sure you, inside <application>, have something like this:

在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 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>

其中YourServiceSubClassName扩展android.app.Service是你的java类服务。其中com.some.package是包名,对我来说都是AndroidManifest.xml和Java。使用javabeat.net文章作为帮助,查找

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.

另请注意,假设在包名和类名之间应该有.service。在文中,我想这是一些约定,但对我来说,这导致我还没有解决的ClassNotFoundException。

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.

然后,安装你的apk。我是从eclipse做的,但adb install -r yourApkHere.apk应该可以工作。卸载是adb uninstall com.some.package.name,顺便说一下。

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

你可以从这样的主机系统启动它,感谢Just a Tim和MrRoy:

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

interestingly, I didn't need -n.

有趣的是,我不需要-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).

由于我是初学者,请感到*编辑/评论以修复任何误解(例如,可能关于组件(?)名称中的.service。)。

#2


13  

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.

开始服务。选项包括: - user | current:指定要运行的用户;如果未指定,则以当前用户身份运行。

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.

停止服务。选项包括: - user | current:指定要运行的用户;如果未指定,则以当前用户身份运行。

#3


12  

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

如果你想在adb shell中运行脚本,那么我试图做同样的事情,但是有一个应用程序。我想你可以使用“am start”命令

usage: am [subcommand] [options]

用法: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>]

#4


8  

I can start service through

我可以开始服务了

am startservice com.xxx/.service.XXXService

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

但我不知道如何阻止它。

#5


5  

Responding to pzulw's feedback to sandroid about specifying the intent.

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

The format of the component name is described in the api docs for ComponentName.unflattenFromString

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

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

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

#6


5  

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:

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

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

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

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

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

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

有关android:exported属性的更多信息,请参阅此页面。

#7


2  

am startservice <INTENT>   

我是startservice

or actually from the OS shell

或者实际来自OS shell

adb shell am startservice <INTENT>

adb shell am startservice

#8


2  

You need to add android:exported="true" to start service from ADB command line. Then your manifest looks something like this:

您需要添加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 -->

And then from ADB

然后来自亚行

To start service:

开始服务:

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

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

To stop service (on Marshmallow):

停止服务(在Marshmallow上):

adb shell am stopservice com.your.package.name/.YourServiceName

adb shell am stopservice com.your.package.name/.YourServiceName

To stop service (on Jelly Bean):

停止服务(在Jelly Bean上):

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

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

#9


0  

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

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

And a full path to the service of: app.package.name.example.package.path.MyServiceClass

以及服务的完整路径: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

#10


-1  

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 startservice ...启动服务。选项包括: - user | current:指定要运行的用户;如果未指定,则以当前用户身份运行。停止服务:

adb shell am stopservice ...

adb shell am stopservice ...

#1


34  

I'm a beginner in Android, but got it working like this:

我是Android的初学者,但它的工作原理如下:

in AndroidManifest.xml, make sure you, inside <application>, have something like this:

在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 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>

其中YourServiceSubClassName扩展android.app.Service是你的java类服务。其中com.some.package是包名,对我来说都是AndroidManifest.xml和Java。使用javabeat.net文章作为帮助,查找

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.

另请注意,假设在包名和类名之间应该有.service。在文中,我想这是一些约定,但对我来说,这导致我还没有解决的ClassNotFoundException。

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.

然后,安装你的apk。我是从eclipse做的,但adb install -r yourApkHere.apk应该可以工作。卸载是adb uninstall com.some.package.name,顺便说一下。

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

你可以从这样的主机系统启动它,感谢Just a Tim和MrRoy:

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

interestingly, I didn't need -n.

有趣的是,我不需要-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).

由于我是初学者,请感到*编辑/评论以修复任何误解(例如,可能关于组件(?)名称中的.service。)。

#2


13  

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.

开始服务。选项包括: - user | current:指定要运行的用户;如果未指定,则以当前用户身份运行。

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.

停止服务。选项包括: - user | current:指定要运行的用户;如果未指定,则以当前用户身份运行。

#3


12  

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

如果你想在adb shell中运行脚本,那么我试图做同样的事情,但是有一个应用程序。我想你可以使用“am start”命令

usage: am [subcommand] [options]

用法: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>]

#4


8  

I can start service through

我可以开始服务了

am startservice com.xxx/.service.XXXService

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

但我不知道如何阻止它。

#5


5  

Responding to pzulw's feedback to sandroid about specifying the intent.

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

The format of the component name is described in the api docs for ComponentName.unflattenFromString

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

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

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

#6


5  

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:

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

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

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

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

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

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

有关android:exported属性的更多信息,请参阅此页面。

#7


2  

am startservice <INTENT>   

我是startservice

or actually from the OS shell

或者实际来自OS shell

adb shell am startservice <INTENT>

adb shell am startservice

#8


2  

You need to add android:exported="true" to start service from ADB command line. Then your manifest looks something like this:

您需要添加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 -->

And then from ADB

然后来自亚行

To start service:

开始服务:

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

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

To stop service (on Marshmallow):

停止服务(在Marshmallow上):

adb shell am stopservice com.your.package.name/.YourServiceName

adb shell am stopservice com.your.package.name/.YourServiceName

To stop service (on Jelly Bean):

停止服务(在Jelly Bean上):

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

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

#9


0  

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

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

And a full path to the service of: app.package.name.example.package.path.MyServiceClass

以及服务的完整路径: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

#10


-1  

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 startservice ...启动服务。选项包括: - user | current:指定要运行的用户;如果未指定,则以当前用户身份运行。停止服务:

adb shell am stopservice ...

adb shell am stopservice ...