Search code examples
androidandroid-intentandroid-manifestreceiver

Where put the intent?Java or Manifest?


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?


Solution

  • 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().