24

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>

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 ?


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

Martin Trigaux
  • 5,311
  • 9
  • 45
  • 58

3 Answers3

33

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.

Use

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

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.

  • Thanks, nice way to go. Maybe I can also create my own event that only my application can catch to avoid conflict with other 3rd party apps. – Martin Trigaux Dec 14 '11 at 00:38
  • 1
    @MartinTrigaux Just checked the docs and remembered that it's possible to specify a component to achieve that. See my edit. –  Dec 14 '11 at 00:51
2

Option 1: Add a dummy activity that will start your service. Remove it before shipping.

Option 2: Create a second test application that starts your service.

EboMike
  • 76,846
  • 14
  • 164
  • 167
  • How will the app behave at runtime? What will make the application run? Running after boot isn't good enough, as Android may kill the process. What will initiate it again? – AlikElzin-kilaka Jun 16 '13 at 17:47
1

Starting The service using Broadcast receiver through any Intent like

android.intent.action.BOOT_COMPLETED

is not possible above Android 3.1 until the user uses your app, and thus to use your app there must be a single non UI activity (which can call finish in oncreate()).

See the proof here

Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61