Monday, June 20, 2011

Using SharedPreferences in Android Properly

The use of SharedPreferences outside of an Activity Class can become very tedious. This is because PreferenceManager.getDefaultSharedPreferences(Context context) requires a context object. In order to simplify things an easy solution is to use the Application object like this.

public class MyApplication extends Application {

   public static SharedPreferences settings;

   public void onCreate() {

This code will work because the Application object is created before all other activities, hence the initialization is guaranteed. Now the SharedPreferences can be simply statically accessed via MyApplication.settings.

Saturday, June 18, 2011

Nexus One USB Debugging with Ubuntu

In order to connect the Nexus one to Ubuntu via USB you need to have the file
with the following line:
SUBSYSTEM=="usb", SYSFS{idVendor}=="18D1", MODE="0666"
You might have to restart Ubuntu, for the settings to take effect.

Then follow theses steps:

1. Disconnect the Nexus One.

2. cd to {android-sdk}/platform-tools

3. Shutdown the daemon with
sudo ./adb kill-server
Sometimes it is necessary to run the command twice to shutdown the server.

4. Restart the daemon with
sudo ./adb devices

5. Connect the Nexus One, make sure it is in debugging mode (Settings>Applications>Development)

6. Check that Nexus One was recognized:
sudo ./adb devices