Archive for the 'Code snippets' Category

SD Cards and the Emulator

Posted in Code snippets on April 25th, 2008 by matt

From a command prompt / terminal navigate to the android sdk’s tools folder.

Resetting the emulator

To reset the emulator and wipe all the stored data, returning as it were to factory settings run:

emulator -wipe-data

Create a blank SD card image:

mksdcard <size> <filename>

e.g. mksdcard 512M halfagig.sd

will create a 512 MegaByte sdcard image called halfgig.sd.

Mounting an SD Card in the emulator:

emulator -sdcard <filepathandname>

This will mount the card under the path /sdcard

Mounting an SD Card image under Linux:

mount -o loop [filename] [/media/mountpoint]

How to capture key pad presses in an Activity.

Posted in Code snippets on April 23rd, 2008 by matt

To capture the keypad presses and any other keys on the phone…
Override the Activities OnKeyDown and compare the keycode against one of the constants in KeyEvent.KEYCODE_*

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent ev) {
                switch (keyCode) {
                case KeyEvent.KEYCODE_DPAD_LEFT:
                        // do something
                        return true;
                case KeyEvent.KEYCODE_DPAD_RIGHT:
                        // do something
                        return true;
                default:
                                return super.onKeyDown(keyCode, ev);
                }
        }
 

A couple of things to note are, returning true informs the caller that you have handled the keypress and it does not need to propagate to other views. Don’t forget to return the result of super.onKeyDown as the last resort or you’ll find out the default handling of keys no longer gets processed for your activity. (e.g. the back button will no longer navigate back through the Activity stack)