我想在Android设备开始对USB或AC充电时收到中断 我通过以下代码注册了接收器 -
registerReceiver(powerMonitor,newIntentFilter("android.intent.action.ACTION_POWER_CONNECTED"));
接受广播的代码是 -
public void onReceive(Context context, Intent intent) {
powerMonitor = new BroadcastReceiver() {
public void onReceive(Context context,Intent intent) {
if (intent.getAction().equals("android.intent.action.ACTION_POWER_CONNECTED"))
{
msg = "power connected";
}
问题是,当我通过USB线连接设备时,我无法获得中断,如果我尝试移除并插入USB线,则VM会断开连接。如何检查? 我错了吗? 我甚至尝试了以下代码 -
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(broadcastReceiver, filter);
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
if(plugged==BatteryManager.BATTERY_PLUGGED_AC ||plugged==BatteryManager.BATTERY_PLUGGED_USB)
{
msg="power connected"
}
请帮助!!
答案 0 :(得分:0)