Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

UsbManager and "this"

Options
  • 22-11-2012 3:36pm
    #1
    Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭


    hey couple of questions from a newbie.
    I am making an android table connect to serial devices. When i call onResume() (yet to write that method) I want to check if there are new devices so I have something like this below. My question is that does that look right, do i need to return if the iterator is empty like that?

    Another simpler question is for the code below it. I am really confused about how "this" is working here. What is the the difference between my two segments of code? One has "this" littered throughout and the other does not. I'm just a bit confused as to whether it matters. I've seen code with both ways.
    public class MainActivity extends Activity {
    
    	private UsbManager mUsbManager;
    	private ArrayList<UsbDevice> mDeviceList = new ArrayList();
    
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		Intent intent = new Intent(this, Term.class);
    		UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
    		[B]this[/B].mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    	}
    
    	public void checkForDevices() {
    		Iterator<UsbDevice> localIterator = null;
    		if ([B]this.[/B]mUsbManager != null) {
    			localIterator = [B]this[/B].mUsbManager.getDeviceList().values().iterator();
    		}
    		if (!localIterator.hasNext()) {
    			return;
    		}
    		while (localIterator.hasNext()) {
    
    			UsbDevice localUsbDevice = (UsbDevice) localIterator.next();
    			if (localUsbDevice.getVendorId() == 1659) {
    				[B]this[/B].mDeviceList.add(localUsbDevice);
    			}
    
    		}
    
    	}
    }
    

    public class MainActivity extends Activity {
    
    	private UsbManager mUsbManager;
    	private ArrayList<UsbDevice> mDeviceList = new ArrayList();
    
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    		Intent intent = new Intent(this, Term.class);
    		UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
    		mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    	}
    
    	public void checkForDevices() {
    		Iterator<UsbDevice> localIterator = null;
    		if (mUsbManager != null) {
    			localIterator = mUsbManager.getDeviceList().values().iterator();
    		}
    		if (!localIterator.hasNext()) {
    			return;
    		}
    		while (localIterator.hasNext()) {
    
    			UsbDevice localUsbDevice = (UsbDevice) localIterator.next();
    			if (localUsbDevice.getVendorId() == 1659) {
    				mDeviceList.add(localUsbDevice);
    			}
    
    		}
    
    	}
    }
    


    Another thing that has confused me in examples is this, what is the difference between these two?
    mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
    mUsbManager = (UsbManager) getSystemService("usb");
    


Comments

  • Registered Users Posts: 18,272 ✭✭✭✭Atomic Pineapple


    this references the context you are currently using, so in an Activity this refers to that Activity, in an Activity you general don't have to actually use it, so for example getString(...) works the same as this.getString(...).

    However outside of something that doesn't have a context like a Java Class you will have to pass it a context and use that for the context methods.

    Context.USB_SERVICE is just a static variable for the String "usb" so it just returns "usb" instead of you having to use the String "usb".


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    Great thanks! I always was a bit shaky when I was using them in java heh. I thought I wouldn't have to use it in an activity so it was confusing when some people do and some people don't. I find it much harder to test things in android than standard java.


Advertisement