Posts Tagged ‘Windows Mobile’

Locatie gebaseerde NS Reisplanner

De Nederlandse Spoorwegen heeft sinds enkele maanden een mobile website (http://m.ns.nl) waarmee je een reis kunt plannen maar ook de actuele vertrektijden kan opvragen van treinen die vanaf een bepaald station vertrekken. Handig om op je mobiel te kijken of de trein vertraging heeft.

Een nadeel van een mobiele telefoon vind ik persoonlijk dat het intypen van tekst toch enige ervaring/tijd vereist. Op het moment dat ik klaar ben met het station in te typen voor de actuele vertrektijden is de trein al gearriveerd (of al drie keer langsgeweest).

Google heeft afgelopen vrijdag een nieuwe versie van Google Gears uitgebracht (http://gears.google.com). Google Gears biedt onder andere als uitbreiding op je webbrowser de mogelijkheid om gegevens van een website tijdelijk offline beschikbaar te stellen (indien de website hiervan gebruik maakt). In de nieuwe versie is Google Gears ook voorzien van een Geolocatie module. Met behulp van deze module kun je aan de hand van het ipadres een grove schatting maken van de locatie van de gebruiker. Deze informatie kan je gebruiken in een webpagina (de gebruiker zal Google Gears geïnstalleerd moeten hebben en zal bij het eerste bezoek de vraag krijgen of je de locatie gegevens wilt delen). Google Gears werkt buiten de desktop ook op Windows Mobile.

Google Gears, Windows Mobiles, NS Reisplanner… dat biedt de mogelijkheid om een lijstje stations weer te geven aan de hand van de locatie. De afwijking, 25 km (wat ik nu op mijn mobiel kreeg), is hierbij geen probleem omdat ik maximaal 10 stations binnen die cirkel (gesorteerd op degene welke het dichtste bij is) weergeef. Door een station te selecteren zal vervolgens de actuele vertrektijden pagina van het betreffende station geopend worden (wat gewoon de pagina van de mobiele NS website is).

Uiteraard is zo’n verhaal erg boeiend, maar uitproberen is altijd leuker: http://www.johandekoning.nl/treinplanner. Google Gears moet hiervoor zijn geinstalleerd maar als dit niet het geval is zal er een download link verschijnen.

Binnenkort zal ik in een nieuw (Engels) blog item de technische details vertellen over de werking van deze locatie gebaseerde treinplanner

Vandaag (27 augustus) de applicatie getest onderweg van huis naar het werk. Bij Amsterdam Amstel was de afwijking volgens Google Gears maar 500 meter. Het lijkt er naar mijn mening op dat bij de smartphones ook gebruik wordt gemaakt van de netwerk gegevens (afstand tot de mast).

HttpWebRequest on Compact Framework: The operation has timed-out

With .Net Compact Framework I developed an application that is doing a webrequest on interval bases. With the webrequest I sent data by providing some arguments inside the querystring. I am not interested in the response and therefore I did not assign the GetResponse() return value. I use the following code:

HttpWebRequest webRequest;
try
{
   webRequest = WebRequest.Create("http://someurl.com/with.aspx?querystring=value");
   webRequest.Method = "GET";
   webRequest.KeepAlive = false;
   webRequest.GetResponse();
}
catch (Exception e){
    //Something goes wrong. Implement handling of this exception
}
finally {
   webRequest = null; //To clear up the webrequest
}

This code works… for only 2 times. The third time it is executed I get a: The operation has timed-out exception. It looks like the GetResponse() method keeps a connection (or socket) open which make it not possible to make new connection at the third time the method is executed. I solved this problem by assigning the GetResponse() value to a WebRequest variable and execute the Close() method inside the finally part of the try catch:

HttpWebRequest webRequest;
WebResponse webResponse = null; //Need to assign this as null otherwise it does not compile
try
{
   webRequest = WebRequest.Create("http://someurl.com/with.aspx?querystring=value");
   webRequest.Method = "GET";
   webRequest.KeepAlive = false;
   webResponse = webRequest.GetResponse(); //Assign to webResponse
}
catch (Exception e){
    //Something goes wrong. Implement handling of this exception
}
finally {
   if(webResponse != null)
   {
      webResponse.Close(); //Close webresponse connection
   }
   webResponse = null; //To clear up the webresponse
   webRequest = null; //To clear up the webrequest
}

Command-line arguments Windows Mobile

In September I will go on holiday to the United States for 3 weeks. It would be nice to send updates to family and friends by placing images on Picasa. These web images will be taken with the photocamera from my mobile (which is Windows Mobile 6 based). I will upload the pictures when a free WiFi access point is available.

The program will place taken photos in a queue. When I have an internet connection available I can upload all the pictures from this queue to Picasa. This results in an application that can be seperated in two parts: the take a photo functionality and the upload photo-queue functionality.

Use cases explaining functionality

My mobile has a photo camera button which will trigger the default photo camera application. This default camera application can be replaced by changing the button settings on the mobile phone. When replaced (and when clicking the camera button) I want my application to start up ready to take a picture (so it will behave the same as the default camera application). After taking a picture you can type in some comment and add it to the queue.

When arriving at the hotel (or some other connective place) I want to upload the taken images. I can off course push the photo camera button again, but this time it is not my intention to take a new picture. In this case I want to start the application from the Start -> Programs menu and see the state of the queue with the option to upload the photos. The same application but with a different startup state.

The use of command-line arguments should solve this problem. But is this also possible with the Compact .Net Framework

Not a solution: Environment.GetCommandLineArgs

For Windows Form based application (which my mobile application is) the use of the Environment class is suggested to get the command-line arguments. The static method

<br />
public static string[] GetCommandLineArgs()<br />

The problem is that this method is not implemented inside the .Net Compact Framework and can’t therefore not been used.

Solution: static void Main(String[] args)

Just like console application (written in .Net) you have a Main method for your Windows Forms application. Besides the files created for the Form (by default Form1.cs and Form1.Designer.cs) a Program.cs file is created which contains a Main method. By adding a String[] args to the Main method, you can work with command-line arguments. An example of the new Main method

<br />
[MTAThread]<br />
static void Main(String[] args)<br />
   {<br />
      if (args != null &#038;&#038; args.Length > 0)<br />
     {<br />
         foreach (String arg in args)<br />
         {<br />
            MessageBox.Show(arg);<br />
         }<br />
     }<br />
     else<br />
     {<br />
         MessageBox.Show("No program arguments");<br />
     }<br />
     Application.Run(new Form1());<br />
}<br />

The arguments can be given to the form (Form1 in this example) by changing the constructor.

Creating a shortcut

The easiest way to create a shortcut is by using the Windows Explorer on your mobile phone. Navigate to the executable file of the application. Tap and hold the file selected with your styles to get the context menu. Select copy.

At the same location (or on a different location) tap (and hold) on an empty space to get the context menu. This context menu will give you the option to paste as a shortcut.

To add the command line argument inside the shortcut, i copied the shortcut (.lnk file) with ActiveSync to my desktop pc and edit the content with notepad. The file will now look like this:

59#”\Programmabestanden\ProgramArguments\ProgramArguments.exe” this_is_a_program_argument

My Windows Mobile OS is Dutch. On an English based OS the Programmabestanden part will be replaced by something like Program files. I don’t know if the number 59 before the line is mandatory. The number of characters with quotes is 59 long. So maybe it is used for reading optimalization. I really don’t know. I just added the this_is_a_program_argument at the end of the shortcut, saved the file and copied it back to my pocket pc.

Starting the application with the shortcut will show a messagebox with the text this_is_a_program_argument. When starting the application from the normal executable you will get a messagebox saving No program arguments.

Attachment: example project

You can download the example application from here

Special thanks

I want to thank Paul for discussing this problem and finding a solution.