I've searched a lot but I'm always more confused.
I have to create an app that works on background and checks the battery level. How can I create the Service? The Service should start on the device boot and must communicate with the Activity when the battery reaches a certain percentage, so there isn't any interaction from the consumer.
What type of Service have I to create? What are the things I have to do?
Rather than using a long-running service, use an AlarmManager to trigger alarms periodically, which can be received by a BroadcastReceiver and can call a Service (which should stop itself at some point) or an IntentService to perform your battery check. This will cause the least grief for the user, since it will use less resources to keep your application doing what it needs to do, and less grief for you, since the system will be less likely to kill your endlessly-running app.
For example, for an Alarm Receiver:
For your Boot Receiver:
And for your Service:
In your manifest, you'll need to add in:
You don't need a Service. Include a Broadcast Receiver in your Application that listens for the Intent.ACTION_BATTERY_CHANGED broadcast. Define that broadcast receiver in your Application Manifest and it will be started automatically whenever there is a battery state change.
The broadcasts include information about the current state of the battery so there is no need for an elaborate (and very expensive!) solution to poll for this information.
This page will provide all the details.
An interesting quote from that page:
Other actions you may wish to monitor include:
ACTION_BATTERY_CHANGED
,ACTION_POWER_CONNECTED
,ACTION_POWER_DISCONNECTED