我使用了 ACTION_MEDIA_MOUNTED
和 ACTION_MEDIA_UNMOUNTED
来检测在 Nexus 4
上的 USB
连接,但是我无法接收到任何广播信号。
这是我的广播接收器代码:
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
filter.addDataScheme("file");
debugReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
debugOn = true;
} else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(action)) {
debugOn = false;
}
}
};
registerReceiver(debugReceiver, filter);
有什么想法吗?我也搜索了其他问题;他们说如果我添加
"filter.addDataScheme("file");"
我会收到信号,但我已经尝试过了,没有接收到任何东西。
filter.addDataScheme("file")
吗?我还尝试了Intent.ACTION_POWER_CONNECTED
和Intent.ACTION_POWER_DISCONNECTED
,它们可以被接收到。但我想检测手机连接到电脑的操作。 - Ricky Liintent.getInt(BatteryManager.EXTRA_PLUGGED)
的值仍然等于BatteryManager.BATTERY_PLUGGED_USB
。 - Ricky Li