我有一个旨在仅作为服务运行的应用程序(没有界面,仅在后台运行)。我的 AndroidManifest.xml 中没有提到任何活动,但放置了一个接收器以在手机启动时启动应用程序。
<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)应用程序时,我需要经常测试我的更改。当我运行应用程序时(在调试模式下连接手机),我收到一条消息,例如
[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
如何在运行时启动应用程序,而不必每次都重新启动它?
Edit对于 Android 3.1 或更高版本,这不再可能。Source http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html
除了 EboMike 提到的两个选项之外:您可以随时发送BOOT_COMPLETED
通过命令行广播,而不是重新启动手机。
Use
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED
这将导致类似于实际重新启动后的情况,并且还将触发任何第 3 方应用程序启动接收器。在终端中输入一次后,通常只需按向上箭头键,然后在大多数操作系统上按回车键即可重复输入。或者您可以将其包含在重新安装应用程序后触发的脚本中。
如果您想将广播限制为仅发送到您的应用程序,您还可以指定一个组件:
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -n your.app.packagename/.YourReceiverClassName
这只会将重启广播发送到您的接收器。不会调用所有其他应用程序。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)