启动无活性的Andr​​oid应用程序活性、应用程序、Andr、oid

2023-09-12 06:13:34 作者:放肆微笑、

我有其目的是仅运行作为服务(没有界面,只是在后台运行)的应用程序。我没有在我的Andr​​oidManifest.xml中提及的活动,而是把一个接收器,启动在手机启动应用程序。

I've an application which aims to run only as a service (no interface, just run in background). I have no activity mentioned in my AndroidManifest.xml but put a receiver to start the application at phone start.

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <service
        android:enabled="true"
        android:name=".MyAppService">
        <intent-filter>
            <action
                android:name = "me.myapp.MyAppService">
            </action>
        </intent-filter>
    </service>
    <receiver
        android:enabled="true"
        android:name=".BootReceiver">
        <intent-filter>
            <action android:name = "android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>
</application>

现在的问题是,当我开发(使用Eclipse)的应用程序,我需要经常测试我的变化。当我运行的应用程序(我的电话连接在调试模式下),我有一个消息像

The problem is that as I'm developing (using Eclipse) the application, I need to test my changes often. When I run the application (with my phone connected in debug mode), I've got a message like

[2011-12-14 00:18:40 - MyApp] Android Launch!
[2011-12-14 00:18:40 - MyApp] adb is running normally.
[2011-12-14 00:18:40 - MyApp] No Launcher activity found!
[2011-12-14 00:18:40 - MyApp] The launch will only sync the application package on the device!
[2011-12-14 00:18:40 - MyApp] Performing sync

我如何开始运行的应用程序,而无需重新启动它每一次?

How can I start the application at run, without having to restart it every time ?

修改这是不可能的了针对Android 3.1或更高版本。 Source

Edit this is not possible anymore for Android 3.1 or above. Source

推荐答案

除了通过EboMike提到的两种选择:你可以​​随时发送 BOOT_COMPLETED 通过命令行播出而不是重新启动您的手机。

Apart from the two options mentioned by EboMike: You can always send the BOOT_COMPLETED broadcast via the command line instead of rebooting your phone.

使用

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED

这会导致这样的情况在实际重启后,也将触发任何第三方应用程序启动接收器。在终端一旦输入后,你通常可以简单地通过pressing向上箭头键,然后在大多数操作系统上的回报重复。或者你可以将其包含在重新安装你的应用程序后,引发了脚本,多数民众赞成。

This will result in a situation like after an actual reboot, and will also trigger any 3rd party apps boot receivers. After typing it once in a terminal you can usually repeat it simply by pressing the up-arrow key followed by return on most operating systems. Or you can include it in a script thats triggered after reinstalling your app.

如果你想限制广播只有您的应用程序,你也可以指定一个组件:

If you want to limit the broadcast to your app only, you can also specify a component:

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -n your.app.packagename/.YourReceiverClassName

这将重新启动只能广播到您的接收器。所有其他应用程序都不能叫。

This sends the reboot broadcast only to your receiver. All other apps are not called.