Android – Start a service on boot completed

Android – Start a service on boot completed

In this article, i will explain how it is possible to launch a service that runs in background when the Androids OS boots.

All the magic is done by the the intent action android.intent.action.BOOT_COMPLETED that Android sends in broadcast  when the boot operation is completed.

Of course, to allow our application to catch this action, we have to define our Receiver and use a special permission in our Manifest.

Manifest.xml

Broadcast receiver definition
receiver

Using permission
permission

Service definition
service

autoAvvio.java

broadcastJava

The method onReceive will fire as soon as the intent action BOOT_COMPLETED is catched, and as you can see from the code above, an intent tries to lanch our service defined in the class autoAvvioService.

autoAvvioService.java

onstartCommand

Inside our service class, we have to focus our attention to the method onStartCommand that fires the service.
In this case it starts an activity and shows a short message on the screen, but of course it could start any other operation like a http request, port listening, etc

 

BLOG BY CARMELO MAIOLINO

Android developer, developer of Combian64