Android: ACTION_BATTERY_LOW does not start in the emulator. The recipient registered in the code is not displayed

I saw messages in which registerReceiver was mentioned, to call ACTION_BATTERY_LOW should be called (not defined in the manifest).

 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 the logcat log. I am using an emulator to simulate a low battery condition using telnet 5554 and performing power capacity 10 . I see the battery status in the emulator, but no intent works.

Also, if I need to call registerReceiver() inside an action and I don't call unregisterReceiver on onStop or onDestroy , is everything all right? If all is not well, how will I register a receiver for system intentions, even if my application is not in the foreground? (In addition to using the manifest).

+6
source share
4 answers

In the manifest, you can add the following code.

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

In 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 receive a GOT LOW BATTERY WARNING log message when your battery is low, also try this on your device.

Although the above code may work best, do not use BroadCast for such actions, you can control the battery level as described here . You can identify your battery with a code below which is easier.

 int level = battery.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = battery.getIntExtra(BatteryManager.EXTRA_SCALE, -1); float batteryPct = level / (float)scale; 
+2
source

make sure that: 1. you set the โ€œChargerโ€ connection to โ€œNONEโ€ 2. This is the status of the battery โ€œDischargeโ€

+2
source

Key to disable telnet charging

 power ac off 

If you set the battery level to low capacity

 power capacity 1 

intention will be launched

0
source

You can register ACTION_BATTERY_LOW in the manifest - see my detailed answer here - you cannot register ACTION_BATTERY_CHANGED in the manifest.

The reason you are not receiving it, you probably forgot to declare your receiver in the manifest

Also, if I need to call registerReceiver() inside an action and I don't call unregisterReceiver on onStop or onDestroy , is everything all right?

Not. You must register with onResume and unregister onPause ALWAYS - after onPause your receiver is called, it will NOT work out anyway

If everything is not all right, how will I register a receiver to receive system intentions, even if my application is not in the foreground? (In addition to using the manifest).

Only in manifest

-2
source

All Articles