Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: ACTION_BATTERY_LOW not triggered in emulator. Receiver registered in code, not manifest

I have seen posts where it was mentioned registerReceiver has to be called (not defined in manifest) to receive ACTION_BATTERY_LOW intent.

public class MainActivity extends Activity
{

   @Override
   public void onCreate(Bundle savedInstanceState)
   {
       //....  
      registerReceiver(new BatteryLevelReceiver(), new IntentFilter(
                                              Intent.ACTION_BATTERY_LOW));
   }
   // .......
}

BroadcastReceiver

public class BatteryLevelReceiver extends BroadcastReceiver
{
    private static final String TAG = BatteryLevelReceiver.class.getSimpleName();

    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.d(TAG, "onReceive");
    }
}

I do not see the "onReceive" log statement in logcat. I am using emulator to simulate battery low state, using telnet 5554 and executing power capacity 10. I do see the battery status changing in the emulator but no intent triggered.

Also if I have to call registerReceiver() inside an activity and I do not call unregisterReceiver on onStop or onDestroy, is it okay? If not okay, how will I register for a receiver to receive system intents even when my app is not in foreground? (Apart from using manifest).

like image 253
Prasanna Avatar asked Jan 03 '13 05:01

Prasanna


3 Answers

Make sure that:

  1. you are setting the "Charger connection" to "NONE"
  2. That battery status is "Discharging"
like image 166
Gal Rom Avatar answered Oct 21 '22 23:10

Gal Rom


You can add the following code in your manifest.

<receiver android:name=".yourpackage.BroadCastNotifier" >
  <intent-filter>
    <action android:name="android.intent.action.BATTERY_LOW" />
  </intent-filter>
</receiver>

In your BroadCastNotifier Class

package yourpackage;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class BroadCastNotifier extends BroadcastReceiver {
    private final static String TAG = "BroadCastNotifier"; 
    @Override
    public void onReceive(Context context, Intent intent) {
        String intentAction = intent.getAction();

        if(Intent.ACTION_BATTERY_LOW.equalsIgnoreCase(intentAction)){
            Log.e(TAG, "GOT LOW BATTERY WARNING");          
        }
    }

}

You will recieve the log message GOT LOW BATTERY WARNING when your battery is low, also try this in your device.

Although the above code might work the best way is not to use BroadCast for such actions, you can monitor the battery level as described here. You can determine your battery with the code below which is way easier.

int level = battery.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = battery.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

float batteryPct = level / (float)scale; 
like image 2
Lalith B Avatar answered Oct 22 '22 00:10

Lalith B


The key is to disable charging with the telnet command

power ac off

If you then set the battery level to a low capacity

power capacity 1

the intent will be triggered

like image 1
PhilLab Avatar answered Oct 21 '22 23:10

PhilLab