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

ran*_*ock 47 service android adb

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

n61*_*007 45

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

在AndroidManifest.xml中,确保你在里面<application>有这样的东西:

<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>
Run Code Online (Sandbox Code Playgroud)

其中YourServiceSubClassNameextend android.app.Service是您的java类,即服务.com.some.package包名称在哪里,对我来说都是AndroidManifest.xml和Java.使用javabeat.net文章作为帮助,寻找<service>

还要注意,据说在包名和类名之间应该有.service.文字,我想这是一些惯例,但对我来说这导致ClassNotFoundException我还没有解决.

然后,安装你的apk.我从日食中做过,但也adb install -r yourApkHere.apk应该工作.卸载是adb uninstall com.some.package.name,顺便说一句.

你可以这样的主机系统启动它,感谢Just a TimMrRoy:

adb shell am startservice com.some.package.name/.YourServiceSubClassName
Run Code Online (Sandbox Code Playgroud)

有趣的是,我不需要-n.

停止,我使用

adb shell am force-stop com.some.package.name
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.

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

  • 需要添加`android:exported =“ true”`来显示 (2认同)

bon*_*nyz 19

开始服务:

adb shell am startservice ...
Run Code Online (Sandbox Code Playgroud)

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

停止服务:

adb shell am stopservice ... 
Run Code Online (Sandbox Code Playgroud)

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


小智 13

如果你想在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>]
Run Code Online (Sandbox Code Playgroud)

  • 该帮助页面确实无法帮助您弄清楚如何指定意图.特别是,<COMPONENT>的语法是神秘的. (10认同)

ROH*_*SHI 11

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

adb shell dumpsys activity services <your package>
Run Code Online (Sandbox Code Playgroud)

例如: adb shell dumpsys 活动服务 com.xyz.something

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

ServiceRecord{xxxxx u0 com.xyz.something.beta/xyz.something.abc.XYZService}
Run Code Online (Sandbox Code Playgroud)

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

adb shell am stopservice <service_name> 
Run Code Online (Sandbox Code Playgroud)

例如:

adb shell am stopservice com.xyz.something.beta/xyz.something.abc.XYZService
Run Code Online (Sandbox Code Playgroud)

同样,启动服务:

adb shell am startservice <service_name>
Run Code Online (Sandbox Code Playgroud)

要访问服务,您的服务(在AndroidManifest.xml中)应设置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>
Run Code Online (Sandbox Code Playgroud)


MrR*_*ROY 10

我可以开始服务了

am startservice com.xxx/.service.XXXService
Run Code Online (Sandbox Code Playgroud)

但我不知道如何阻止它.


小智 7

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

<service android:exported="true" ></service>
Run Code Online (Sandbox Code Playgroud)

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

adb shell am startservice com.package.name/.YourServiceName
Run Code Online (Sandbox Code Playgroud)

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


Kir*_*k g 7

使用时可能会出现错误“ *错误:应用程序在后台*”

adb shell am startservice 
Run Code Online (Sandbox Code Playgroud)

在奥利奥(26岁以上)。这需要前台的服务。使用以下内容。

adb shell am start-foreground-service com.some.package.name/.YourServiceSubClassName
Run Code Online (Sandbox Code Playgroud)


小智 6

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

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

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


Atu*_*tul 5

您需要添加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 -->
Run Code Online (Sandbox Code Playgroud)

然后从亚行

要开始服务:

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

停止服务(在棉花糖上):

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

停止服务(在Jelly Bean上):

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