Android 电池广播

Android Battery Broadcasts

我正在尝试制作电池警报应用程序。我正在阅读 Android 文档,发现我们不需要注册 BroadcastReceiver。我们可以做这样的事情-

IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);

我的问题是-

这是获取电池电量的完整示例

private float getBatteryLevel() {
    Intent batteryStatus = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int batteryLevel = -1;
    int batteryScale = 1;
    if (batteryStatus != null) {
      batteryLevel = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, batteryLevel);
      batteryScale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, batteryScale);
    }
    return batteryLevel / (float) batteryScale * 100;
  }

回答您的问题:

1- 是的,你可以通过 getExtra 获取额外的信息并定义键,BatteryManager 有很多键可以从意图中提取信息。

2- 不,您只需要根据 Activity 或 Fragment 生命周期注册和注销接收器,通常在 onCreate()/onCreateView() 中注册并在 onDestry()/onDestroyView() 中注销。

3- 您可以创建具有简单通知界面的绑定服务。你在上面注册你的接收器,从之前的示例中你可以做这样的事情:

if (getBatteryLevel < 10) {
//show notification or do whatever you want.
}

首先在清单文件中添加意图过滤器:

<receiver android:name=".BatteryLevelReceiver">
  <intent-filter>
    <action android:name="android.intent.action.BATTERY_LOW"/>
    <action android:name="android.intent.action.BATTERY_OKAY"/>
    <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
    <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
  </intent-filter>
</receiver>

在项目中添加新 class 后:

public class PowerConnectionReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                            status == BatteryManager.BATTERY_STATUS_FULL;

        int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
    }
}

获取电池电量:

IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);

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

float batteryPct = level / (float)scale;

更多信息link:developer.android

创建接收器以获取信息。这是更好的方法。

public class PowerConnectionReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                            status == BatteryManager.BATTERY_STATUS_FULL;

        int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
    }
}

在清单中添加

<receiver android:name=".PowerConnectionReceiver">
  <intent-filter>
    <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/>
    <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/>
   <action android:name="android.intent.action.BATTERY_LOW"/>
   <action android:name="android.intent.action.BATTERY_OKAY"/>
  </intent-filter>
</receiver>

更多信息请访问LINK

Q1. Yes, system provide every time . 
Example :    
      private class BatteryBroadcastReceiver extends BroadcastReceiver {
            private final static String BATTERY_LEVEL = "level";
            @Override
            public void onReceive(Context context, Intent intent) {
                int level = intent.getIntExtra(BATTERY_LEVEL, 0);
                mBatteryLevelText.setText(getString(R.string.battery_level) + " " + level);
                mBatteryLevelProgress.setProgress(level);
            }
        }


//Register our BroadcastReceiver in application

    BatteryBroadcastReceiver mReceiver = new BatteryBroadcastReceiver();
    registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));


//Update your progress bar get 

    public class MainActivity extends Activity {
        private TextView mBatteryLevelText;
        private ProgressBar mBatteryLevelProgress;
        private BroadcastReceiver mReceiver;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mBatteryLevelText = (TextView) findViewById(R.id.textView);
            mBatteryLevelProgress = (ProgressBar) findViewById(R.id.progressBar);
            mReceiver = new BatteryBroadcastReceiver();
        }
        @Override
        protected void onStart() {
            registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
            super.onStart();
        }
        @Override
        protected void onStop() {
            unregisterReceiver(mReceiver);
            super.onStop();
        }
        private class BatteryBroadcastReceiver extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {
                int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
                mBatteryLevelText.setText(getString(R.string.battery_level) + " " + level);
                mBatteryLevelProgress.setProgress(level);
if(level<10){
// play Battery alarm. 

            }
        }
    }