0

Something is still not clear for me; I have to monitoring the battery level and i wrote inside my service in th onCreate this lines:

public void onCreate(){
   super.onCreate();
   mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
   new IntentFilter(Intent.ACTION_BATTERY_CHANGED);    
}

Then in the Manifest:

<receiver android:name=".ReceversAndServices.BatteryLevelReceiver">
  <intent-filter>
    <action android:name="android.intent.action.BATTERY_CHANGED" />
    <action android:name="android.intent.action.BOOT_COMPLETED" />
  </intent-filter>
</receiver>

What i don't understand is.. Is it correct write the Intent Intent.ACTION_BATTERY_CHANGED in the java and also android.intent.action.BATTERY_CHANGED in the Manifest? Or just need only one?

B770
  • 1,272
  • 3
  • 17
  • 34
David_D
  • 1,404
  • 4
  • 31
  • 65

3 Answers3

3

Whenever you are planning on using an Intent Filter, always check with the Intent documentation. If the broadcast cannot be picked up by the manifest Intent Filter, it will usually be described in the documentation. For example, the BATTERY_CHANGED broadcast action provides this information:

You can not receive this through components declared in manifests, only by explicitly registering for it with Context.registerReceiver().

A--C
  • 36,351
  • 10
  • 106
  • 92
  • You suggest to leave the java code as i wrote before or have i to change something? – David_D Aug 21 '13 at 15:14
  • 1
    @David_D you will have to actually register the `BroadcastReceiver`, all you have done at the moment is declared the `IntentFilter`. See [this SO](http://stackoverflow.com/a/4805733) answer for more details. – A--C Aug 21 '13 at 15:15
  • Thanks.. But why have i to unregister the receiver onDestroy? Have i to do it also if it's a service? – David_D Aug 21 '13 at 15:17
  • 1
    @David_D Unregistering is to avoid leaking the BroadcastReceiver, which LogCat will show (although the app shouldn't crash). I'd consider it good practise to also unregister in a Service. – A--C Aug 21 '13 at 15:20
  • Do you think that writing the intent in both files could be a battery drain problem? Because now if the notification is activated, eat a little bit battery..and, it's not the best thing for an battery saver application:) – David_D Aug 21 '13 at 15:21
  • 1
    @David_D In what other file? The manifest declaration is useless since the system will ignore it. I suggest you test your current implementation and if that isn't satisfactory, [this SO answer](http://stackoverflow.com/a/7626286) provides some alternatives that don't even use `BATTERY_CHANGED`. – A--C Aug 21 '13 at 15:26
1

You only need 1. Writing it in the manifest makes it active as soon as the app is installed. Putting it in the java code will make it active only when the activity/service its created in is running.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • In this case is a notification.. Do you think it's better write it only in the java? The notification gets the battery level and it's a service so still running even if the application is closed. – David_D Aug 21 '13 at 15:11
1

Both ways are correct. You can write either in Manifest file or in java file.
Some permissions must be write in Manifest file.

tj's
  • 144
  • 3