问题
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).
回答1:
Make sure that:
- you are setting the "Charger connection" to "NONE"
- That battery status is "Discharging"
回答2:
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;
回答3:
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
回答4:
You can register ACTION_BATTERY_LOW
in the manifest - see my detailed answer here - you can't register ACTION_BATTERY_CHANGED
in the manifest.
The reason you do not receive it is probably you forgot to declare your RECEIVER in the manifest
Also if I have to call
registerReceiver()
inside an activity and I do not callunregisterReceiver
ononStop
oronDestroy
, is it okay?
No. You should register in onResume
and unregister on onPause
ALWAYS - after onPause
is called your receiver WON'T receive anyway
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).
Only in manifest
来源:https://stackoverflow.com/questions/14133435/android-action-battery-low-not-triggered-in-emulator-receiver-registered-in-co