我知道通过使用意图过滤器可以获得WiFi和蓝牙的功能 - > ACTION_ACL_CONNECTED,ACTION_ACL_DISCONNECT_REQUESTED和ACTION_ACL_DISCONNECTED ......但是通常有什么东西可以检查是否已删除任何类型的连接设备。例如:我已将我的Android设备连接到我的Mac,现在智能手机已锁定,我删除了与我的Mac的连接,我可以触发本地推送或声音或此方向......
更新
尝试了@Rajesh Gopu的回答,看起来不错,但遗憾的是没有工作:(
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");
}
}
};
}
答案 0 :(得分:2)
Use Broadcaster receiver , Android system informs the internel events by brodcasting it . THis can be received by App's by registering the action with broadcastrecever .
Below is an exmaple to recive usb connect/disconnect event.
IntentFilter filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(mUsbReceiver, filter); //register
// 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
String action = intent.getAction();
System.out.println("BroadcastReceiver Event");
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
System.out.println("BroadcastReceiver USB Connected");
} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
System.out.println("BroadcastReceiver USB Disconnected");
}
}
}