我正在尝试制作电池警报应用程序。我正在阅读Android文档,发现我们不需要注册BroadcastReceiver。我们可以这样做 -
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
我的问题是 -
答案 0 :(得分: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-不,您只需注册接收者并根据活动或片段生命周期取消注册,通常在onCreate()/onCreateView()
注册并在onDestry()/onDestroyView()
取消注册。
3-您可以创建具有简单通知界面的绑定服务。你在上面注册你的接收器,从上一个样本你可以做这样的事情:
if (getBatteryLevel < 10) {
//show notification or do whatever you want.
}
答案 1 :(得分: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
答案 2 :(得分: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;
}
}
在Manifest中添加
<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
答案 3 :(得分: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.
}
}
}