Alert after Alert in J2ME

I just discovered a very nasty restriction in J2ME concerning Alerts whilst developing a MIDP 2.0 application for a Nokia E70 Mobile Phone (running Symbian 60 3rd Edition). As has become common now, the code I had worked find in the Sun Wireless Toolkit emulator but not so on the E70. Anyway, here’s the deal …

If you display an Alert object (e.g. a Yes/No dialog), and then (as a result of the user selecting Yes or No) try to display another Alert object after that, the phone will generate an IllegalArgument exception. This is because of the default behaviour of the Display.setCurrent(Displayable) method which assumes that you want to return to the object that created the Alert after the new alert is dismissed. Now, in my view, this is a reaonable thing to want to do. However, the exception that is thrown suggests that an “Alert cannot return to an Alert” which is not documented anywhere that I’ve seen.

So, the solution was to use the Display.setCurrent(Alert,Displayable) method and ensure that the second parameter is never another Alert.

Thanks to Naomi and Brian for their help on this.

“OK to use Airtime” on Mobile Emulators

If you are developing a J2ME application that connects to the Internet and are using a Mobile Emulator then you will invariably have to put up with the repeated messages that ask if it is OK for your phone to use Airtime (i.e. connect to the Internet). Not only is this very annoying but it also has the potential to prevent proper automated testing from being carried out.

This actually happens when your application has not been properly signed but only seems to happen in the Emulator (it does not happen on my Nokia E70). Until now, I was of the impression that the only way around this was to purchase a full digital signing pack from the likes of VeriSign etc. Gladly, this is not the case …

We overcame this problem using the latest Sun Java Wireless Toolkit and NetBeans as follows:

  1. Start the Sun Java Wireless Toolkit “KToolbar” application and select Edit, Preferences
  2. Locate the Security category and select the following settings
    • Security Policy: MSA
    • Security Domain: manufacturer
  3. Save these preferences
  4. From your NetBeans project, select the project properties.
  5. Locate the Signing settings (inside the Build settings) and select the following settings:
    • Sign Distributon: Yes (or checked)
    • Keystore: Built-in keystore
    • Alias: untrusted
  6. You might also need to Export this key to the Java SDK/Emulator using the button provided
  7. Save these settings and rebuild your project

Hopefully the next time you launch your application from the emulator will no longer ask permission to use Airtime.