1. Are you ready for the Galaxy S20? Here is everything we know so far!

Doubts about BroadcastReceiver

Discussion in 'Android Development' started by pilar, Oct 23, 2018.

  1. pilar

    pilar Lurker
    Thread Starter

    Hi I am using the following code
    Code (Text):
    1. private boolean statusUSB = false;
    2.  
    3. private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {
    4.  
    5.     public void onReceive(Context context, Intent intent) {
    6.         final String action = intent.getAction();
    7.  
    8.         if (action != null) {
    9.  
    10.             switch (action) {
    11.                 case UsbManager.ACTION_USB_DEVICE_DETACHED:
    12.                     final UsbDevice detDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
    13.                     final String detMsg="Device DEtached";
    14.                     statusUSB = false;
    15.                     Toast.makeText(context, "USB Disconnected", Toast.LENGTH_SHORT).show();
    16.                     break;
    17.                 case UsbManager.ACTION_USB_DEVICE_ATTACHED:
    18.                 case UsbManager.ACTION_USB_ACCESSORY_ATTACHED:
    19.                     final UsbDevice attDevice = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
    20.                     final String attMsg="Device atached";
    21.                     statusUSB = true;
    22.                     Toast.makeText(context, "USB Connected", Toast.LENGTH_SHORT).show();
    23.                     break;
    24.                 default:
    25.                     break;
    26.             }
    27.         }
    28.     }
    29. };
    30.  
    To detect when the USB port is connected or disconnected, apart from that I have also inserted a flag:
    Code (Text):
    1. statusUSB
    Code (Text):
    1. @Override
    2. public boolean onCreateOptionsMenu(Menu menu) {
    3.  
    4.     getMenuInflater().inflate( R.menu.main, menu );
    5.  
    6.     if (statusUSB == true){
    7.         // Opcion1
    8.     }
    9.     if (statusUSB == false){
    10.         //Opcion2
    11.     }
    12.  
    13.     return true;
    14. }
    15.  
    The fact is that the Android detects both conditions when the USB port is connected and disconnected, but the flag does not change or I can not read this value in my menu, I am doing well? or there is another way to feed this flag?
     


  2. Deleted User

    Deleted User Guest

    Here's a question for you - out of the following methods, which would you say gets executed first?

    onReceive()
    onCreateOptionsMenu()
     
  3. pilar

    pilar Lurker
    Thread Starter

    The onCreateOptionsMenu() method is the first...
     
  4. Deleted User

    Deleted User Guest

    Probably a better question to ask is, how many times is onCreateOptionsMenu() called?
    Hint: If it's only called once, how would it ever know about changes in the statusUSB variable?
     
Loading...

Share This Page

Loading...