1

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

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

我的问题是——

  • 每次电池电量变化时系统是否广播意图(包含电池状态)?
  • 我每次都必须查询电池电量吗?
  • 此外,当电池电量达到特定水平(由用户设置)时如何播放警报。
4

4 回答 4

2

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

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.
}
于 2017-06-30T18:18:43.570 回答
0

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

<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>

在项目中添加新类后:

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;

更多信息这个链接:developer.android

于 2017-07-01T08:54:41.217 回答
0

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

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>

欲了解更多信息,请访问链接

于 2017-07-01T09:06:52.597 回答
0
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. 

            }
        }
    }
于 2017-07-05T06:59:21.360 回答