如何从 adb shell 启动和停止 android 服务? [英] How to start and stop android service from a adb shell?

查看:23
本文介绍了如何从 adb shell 启动和停止 android 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

我是 Android 的初学者,但它的工作方式如下:

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

在 AndroidManifest.xml 中,确保您在 中具有如下内容:

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>

其中 YourServiceSubClassName 扩展 android.app.Service 是作为服务的 java 类.其中 com.some.package 是包名,对我来说在 AndroidManifest.xml 和 Java 中都是如此.使用 一篇 javabeat.net 文章 作为帮助,看看对于

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>

还要注意,文本中的包名和类名之间应该有 .service. ,我猜这是一些约定,但对我来说这导致了 ClassNotFoundException我还没有解决.

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.

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

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.

您可以像这样从主机系统启动,谢谢只是一个 Tim罗伊先生:

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

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

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

interestingly, I didn't need -n.

为了停止,我使用

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

希望有帮助.

由于我是初学者,请随时编辑/评论以修正任何误解(例如,可能与组件 (?) 名称中的 .service. 相关).

As I'm a beginner, please feel freet to edit/comment to fix any misconceptions (eg. probably regarding .service. in the component (?) name).

这篇关于如何从 adb shell 启动和停止 android 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆