我知道有一些WiFi和蓝牙通过使用意图过滤器 - > ACTION_ACL_CONNECTED,ACTION_ACL_DISCONNECT_REQUESTED和ACTION_ACL_DISCONNECTED ...但是有什么一般来检查是否有任何一种连接的设备已被删除。例如:我已将我的Android设备连接到我的Mac,现在智能手机已锁定,并且移除了与我的Mac的连接,并且我可以触发本地推送或声音或此方向的任何内容...有没有办法确定是否有任何外围设备已被删除?
更新
试图从@Rajesh Gopu这看起来不错,但遗憾的是didn't工作:(答案
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(mUsbReceiver, filter); //register
Toast.makeText(getApplicationContext(), "ON CREATE", Toast.LENGTH_LONG).show();
}
// BroadcastReceiver when insert/remove the device USB plug into/from a USB port
BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {//get Event
Toast.makeText(getApplicationContext(), "ON RECEIVE", Toast.LENGTH_LONG).show();
String action = intent.getAction();
System.out.println("BroadcastReceiver Event");
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
Toast.makeText(getApplicationContext(), "CONNECTED", Toast.LENGTH_LONG).show();
System.out.println("BroadcastReceiver USB Connected");
} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
Toast.makeText(getApplicationContext(), "DISCONNECTED", Toast.LENGTH_LONG).show();
System.out.println("BroadcastReceiver USB Disconnected");
}
}
};
}
“我已经与我的Android设备我的Mac” - 连接它...怎么样? – CommonsWare
没关系。我的意思是任何一种连接/断开听众......蓝牙和WiFi很容易听,如果它已被断开,但如何与USB电缆连接例如。 – BigPun86
“不重要” - 是的,它的确如此。例如,没有听众确定您是否通过一串字符串将设备连接到Mac。 Android中这种性质的API往往侧重于连接技术,而不是连接的目标。 “如何与USB电缆连接” - 这可能取决于电缆的具体使用情况('adb'?MTP用于将外部存储装置作为音量?USB主机/附件API?其他?)。 – CommonsWare