如何启动和从亚行的shell停止Android的服务?shell、Android

2023-09-05 07:45:06 作者:亡城孤影

我需要写一个shell脚本来启动和停止的机器人服务。

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文章的帮助下,寻找&LT;服务&GT;

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>

另请注意,所谓的包名和类名之间应该有。服务。的文字,我想这是一些约定,但对我来说这引起 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安装-r yourApkHere.apk 应该工作。卸载是亚行卸载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.

您可以开始从主机系统就是这样,感谢只是添和< A HREF =htt​​p://stackoverflow.com/a/8469325/611007> MrRoy :

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

要停止,我用

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