In the Activity that has this menu do the following:
Declare a class derived from BroadcastReceiver
public class UsbDeviceDetect extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_CONNECTED")) {
//Aqui torne enable o seu item de menu
}
if (intent.getAction().equalsIgnoreCase( "android.intent.action.UMS_DISCONNECTED")) {
//Aqui torne disable o seu item menu
}
}
}
Declare two attributes, one for the BroadcastReceiver
and another to the IntentFilter
private UsbDeviceDetect usbDeviceDetect;
private IntentFilter filter;
In the method onCreate
create an instance of BroadcastReceiver
and its IntentFilter
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
----------
UsbDeviceDetect usbDeviceDetect = new UsbDeviceDetect();
filter = new IntentFilter();
filter.addAction("android.intent.action.UMS_CONNECTED");
filter.addAction("android.intent.action.UMS_DISCONNECTED");
}
In the method onResume
register the BroadcastReceiver
@Override
protected void onResume(){
super.onResume();
registerReceiver(usbDeviceDetect, filter));
}
In the method onPause
do the unregister of BroadcastReceiver
@Override
protected void onPause() {
unregisterReceiver(usbDeviceDetect);
super.onPause();
}