检查设备是否插入
我的应用程序有一个广播接收器来监听ACTION_POWER_CONNECTED
变化,并依次标记屏幕保持。
我所缺less的是应用程序在第一次运行时检查充电状态的能力。 任何人都可以请帮我用代码手动检查充电状态?
感谢CommonsWare这里是我写的代码。
public class PowerUtil { public static boolean isConnected(Context context) { Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB; } }
public static boolean isPlugged(Context context) { boolean isPlugged= false; Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); isPlugged = plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB; if (VERSION.SDK_INT > VERSION_CODES.JELLY_BEAN) { isPlugged = isPlugged || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS; } return isPlugged; }
一个小的更新来支持无线充电。
调用registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED))
。 这将返回一个Intent
,在BatteryManager
上定义了附加function,让您知道它是否插入。
这是因为Intent.ACTION_BATTERY_CHANGED
是一个粘性广播。
你的答案是在Android的参考 !
以下是示例代码:
// Are we charging / charged? int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL; // How are we charging? int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB; boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
简单的方法是使用意图filterACTION_BATTERY_CHANGED
public void checkBatteryState(View sender) { IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = registerReceiver(null, filter); int chargeState = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); String strState; switch (chargeState) { case BatteryManager.BATTERY_STATUS_CHARGING: case BatteryManager.BATTERY_STATUS_FULL: strState = "charging"; break; default: strState = "not charging"; } TextView tv = (TextView) findViewById(R.id.textView); tv.setText(strState); }
尝试这个:
public class PowerConnectionReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = context.registerReceiver(null, ifilter); 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; if (batteryStatus != null) { int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1); float batteryPct = level / (float) scale; } }//end onReceive }//end PowerConnectionReceiver
BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); if(action.equalsIgnoreCase(ACTION_POWER_CONNECTED)) { if(chargerConnected != null ) { chargerConnected.setText("Cable Connected"); } if(chargerImage != null ) { chargerImage.setImageDrawable(getResources().getDrawable(R.drawable.usb)); } }else if(action.equalsIgnoreCase(ACTION_POWER_DISCONNECTED)) { if(chargerConnected != null ) { chargerConnected.setText("NOT CHARGE"); } if(chargerImage != null ) { chargerImage.setImageDrawable(getResources().getDrawable(R.drawable.battery_icon)); } try { Toast.makeText(context, "Power Cable Disconnected", Toast.LENGTH_SHORT).show(); }catch (Exception e){e.printStackTrace();} } } };
这是一个粘性的意图,你不需要注册一个BroadcastReceiver – 通过简单地调用registerReceiver作为接收器传递null作为接收器,如下面的代码片段所示,返回当前的电池状态意图。 您可以在这里传递一个实际的BroadcastReceiver对象,但是我们将在后面的章节中处理更新,所以没有必要。
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = context.registerReceiver(null, ifilter);
您可以提取当前的充电状态,如果设备正在充电,是否通过USB或AC充电器进行充电:
// Are we charging / charged? int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL; // How are we charging? int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB; boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
每当设备连接或断电时,BatteryManager都会广播一个动作。 即使您的应用没有运行,接收这些事件也很重要 – 特别是这些事件会影响您启动应用的频率,以便启动后台更新 – 因此,您应该在清单中注册BroadcastReceiver,以便通过在意图filter中定义ACTION_POWER_CONNECTED和ACTION_POWER_DISCONNECTED 。
<receiver android:name=".PowerConnectionReceiver"> <intent-filter> <action android:name="android.intent.action.ACTION_POWER_CONNECTED"/> <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"/> </intent-filter> </receiver>
在关联的BroadcastReceiver实现中,可以按照上一步中的描述提取当前充电状态和方法。
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; } }