1

Im sure this is a very common problem but have not seen any questions asked on how to accomplish the following.

I created a service that runs every 5 minutes and gets called when the device has finished on BOOT_COMPLETE and this works just fine.

The problem I am encountering is when I am debugging on my device I have to re-boot my phone in order to get the service to launch. Is there a way for me to start this background service that runs every 5 mintues on Application Startup.

So what I need is to check if the service is running in the first place and if it is not start it.

Just to elaborate a little bit more, this service fetches data of the web so my ideal situation of having it get kicked off by BOOT_COMPLETE works just great. But in the scenarios that the user downloads the app and launches it this service will not get kicked off so I need a manual way of starting. If the service is not running, which it wouldn't be if the user just installed it I need to start it.

Byron
  • 3,833
  • 9
  • 34
  • 39

3 Answers3

2

I actually solved my problem by using the approach used in the following link.

How to send and receive broadcast message

Remove the IntentFilter from the Manifest and manually calling it. Hope this helps someone else.

    if(!isMyServiceRunning()) {
        Intent intent = new Intent(mContext, OnBootReceiver.class);
        sendBroadcast(intent); 
    }
Community
  • 1
  • 1
Byron
  • 3,833
  • 9
  • 34
  • 39
1

You can start the service manually with adb shell. See How to start and stop android service from a adb shell?

The command is:

am startservice com.your.package/.ServiceClassName

Depending on the Android version the effective syntax may change slightly, for example:

am startservice -n com.your.package/.ServiceClassName
am startservice -a com.your.package/.ServiceClassName
am startservice --user 0 -n com.your.package/.ServiceClassName
Community
  • 1
  • 1
Piovezan
  • 3,215
  • 1
  • 28
  • 45
1

Sounds like you want to do a sticky service that started by BOOT_COMPLETE event

http://developer.android.com/reference/android/app/Service.html#START_STICKY

For development, you can add another intent filter to your service to start it so you can manually trigger it via activity manager from the command line after each code update without having to restart the device.

Phuong Nguyen
  • 909
  • 7
  • 20
  • i updated my description, please read. do you think a start_sticky will still serve my purpose? – Byron Jun 11 '13 at 20:39
  • You can't start package immediately after installation unfortunately. http://stackoverflow.com/questions/2127044/how-to-start-android-service-on-installation – Phuong Nguyen Jun 12 '13 at 21:47
  • you can broadcast BOOT_COMPLETE as well: `adb shell am broadcast -a android.intent.action.BOOT_COMPLETED` – Alexander Malakhov Jul 29 '13 at 07:49