Tuesday, December 22, 2009

Drawing from a thread

Like the swing implementation, android must only write to the UI if it is on the UI thread.

The recommended way to do this is using the AsyncTask. More information about this can be found here.

Sunday, December 20, 2009

JUnit

One of the first things that I needed to do was run unit tests on the android phone.

The application of choice is obviously Junit, but, you need a few tweaks as you will want to run it on the device and you will also most probably need to obtain resources and contexts.

The first thing to do is make sure that you create a Test project when you create your main application base. This will put all of the test cases that you write into it.

To create a test case, just right click on the class you want to test and create a JUnit test case.

Once the test case is created, you will want access to the resources in the project. Change the test case so that it does not inherit from the regular JUnit test case, instead, use this:


android.test.AndroidTestCase


Now you will be able to write code such as this in the test case:


Resources resources = getContext().getResources();
InputStream inputStream = resources.openRawResource(com.myapp.R.raw.infile);


There is something important to realize here. The context that you obtain is from the application - not the test case. I can't find any way to put test files in the test case project and then access them. There probably is a way, but for now, I am not sure. Instead, if I want to use test data, I will just put test files in the application folder until I find a better way.

To run the test, just right click and do run as android junit test.

One final point. Sometimes the tests don't run the first time you invoke them like this. To fix this, just run the tests again and it seems to pick it up.

Saturday, December 19, 2009

Debugging an application on the device

By far the best way to program for android is with a device. It is faster and easier to do.

To get to this point, there are a couple of things I needed to do.

The first was to get my device ready to host and debug and application. This guide:



  • On the device, go to the home screen, press MENU, select Applications > Development, then enable USB debugging.


  • Setup linux to detect the device. For myself, I added the following to /etc/udev/rules.d/51-android.rules:

    SUBSYSTEM=="usb", SYSFS{idVendor}=="0bb4", MODE="0666"


    Note (you might need to use SUBSYSTEM="udb_device" depending on what version of ubuntu you are using).
    And then:

    chmod a+r /etc/udev/rules.d/51-android.rules



  • execute adb devices. If this doesn't work, you might need to do the following:

    sudo su -
    adb kill-server
    adb start-server
    adb devices




Once you have done this, you should be able to run the application from eclipse.

NOTE: You must remember to enable debugging in the Android Mainfest. This is under the 'Application' tab of the manifest under eclipse.

NOTE: The above was all done with ubuntu 9.10 running eclipse 4.5.1.

Eclipse

I spent considerable time with a frustrating, intermittent problem. Under eclipse, things would hang. In fact, the weren't hanging, the UI was just not updating properly.

A google search didn't turn anything up. I clicked on the 'known issues' link on eclipse and was pleasantly surprised that there was a known issue. GTK and SWT dont play together nicely. To fix, just need export GDK_NATIVE_WINDOWS=true.

Starting up tinkering with andoird again

It has been a while, but I have decided to tinker with android once again now that I have flashy new android toy to play with.

The install process didn't go as smoothly as I would have liked. Firstly, when installing the eclipse ADT plugins, I was unable to access the https site. Switch to http worked (eventually).

Next, I went to have a look at what AVD (android virtual devices) I had access to using the android tool. There were none, so I went to see if I could update. I was unable to do so. Even checking the option to use http instead of https in the settings did not help.

Eventually I found a google group thread that discussed how to update the packages using the android tool:


android update sdk


The readme did point this out, but the web page for getting started did not.

Fingers crossed that this gets me a development environment.