我有带有 BATTERY_CHANGED IntentFilter 的 BroadcastReceiver。当我尝试发送广播时, onReceive 不会调用。怎么了?这是我在清单中的接收器描述
<receiver android:name=".receivers.BatteryReceiver">
<intent-filter>
<action android:name="android.intent.action.BATTERY_CHANGED"/>
</intent-filter>
</receiver>
我的广播接收器
public class BatteryReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("tag", "onRecv");
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
if (level != -1 && scale != -1) {
float batteryPct = level / (float) scale;
if (batteryPct < 30) {
NotificationBuilder.notifyWithActions(context, R.drawable.ic_info_black_24dp,
context.getResources().getString(R.string.app_name),
context.getResources().getString(R.string.battery),
context.getResources().getInteger(R.integer.battery));
}
}
}`}
我试试
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(Intent.ACTION_BATTERY_CHANGED));