devxlogo

Build Your Own Windows Mobile RSS Reader

Build Your Own Windows Mobile RSS Reader

ith the recent introduction of the Windows Mobile 6 platforms, we are now beginning to see a proliferation of new devices supporting the Windows Mobile 6 Standard (aka Smartphone). Because Windows Mobile 6 Standard devices do not have touch screens, they pose certain challenges when developing applications to run on them. This article will show you how to develop a Windows Mobile 6 Standard application, and at the same time, build a useful RSS Reader application that you can actually use if you have one such device.

Building the User Interface
To get started, launch Visual Studio 2005 and create a new Windows Mobile 6 Standard application. Name the application RSSReader.

Author’s Note: You need to download the free Windows Mobile 6 Standard SDK in order to create the application detailed in this article.

Populate the default Form1 with the following controls (see Figure 1):

  • TreeView control
  • MenuItem controls

Add an ImageList control to Form1 and add three images to its Images property (see Figure 2).


Figure 1. Form1: Populating the default Form1 with the various controls.
 
Figure 2. Adding Images: Adding three images to the ImageList control.

Set the ImageList property of the TreeView control to ImageList1.

Add a new Windows Form to the project and populate it with a WebBrowser and MenuItem control (see Figure 3).

Figure 3. Form2: Adding a WebBrowser and MenuItem control to Form2.

Switch to the code behind of Form1 and import the following namespaces:

Imports System.IOImports System.NetImports System.XmlImports System.Text.RegularExpressions

Declare the following constants and variable:

Public Class Form1    '---constants for icons---    Const ICO_OPEN As Short = 0    Const ICO_CLOSE As Short = 1    Const ICO_POST As Short = 2    '---file containing the list of subscribed feeds---    Const FEEDS_LIST As String = "My DocumentsFeeds.txt"    '---used for displaying a wait message panel---    Dim displayPanel As Panel

Creating the Helper Methods
When RSS feeds are being downloaded, you would display a message on the screen so that users know that the application is busy (see Figure 4).

Figure 4. Operation Status: The device displays a message informing the user of the operation’s status.

For this purpose, you can improvise with the aid of the Panel and Label controls.

Define the CreatePanel() function so that you can dynamically create the message panel using a couple of Panel controls and a Label control, as shown in Listing 1.

Next, define the IsConnected() function to test whether the user is connected to the Internet:

    '---check whether you are connected to the Internet---    Private Function IsConnected() As Boolean        Try            Dim hostName As String = Dns.GetHostName()            Dim curhost As IPHostEntry = Dns.GetHostEntry(hostName)            Return curhost.AddressList(0).ToString() <> _               IPAddress.Loopback.ToString()        Catch ex As Exception            Return False        End Try    End Function

Basically, you compare the IP address of the device with that of localhost (127.0.0.1). As long as the device’s IP address is not 127.0.0.1, it is assumed that there is Internet connectivity (as shown in Listing 2).

Each post title and its description are separated by the ASCII character 3 and each posting is separated with the ASCII character 12. Notice that after the XML feed for an URL is downloaded, it is saved onto storage. This is to ensure that the application continues to work in offline mode (i.e. when user disconnects from the Internet). The URL of the feed is used as the filename, minus all the special characters inside within the URL, with the “.xml” extension appended. To strip off all the special characters in the URL, define the RemoveSpecialChars() function as follows:

Figure 5. Adding Posts: Adding each post to the TreeView control.

    '---removes special chars from an URL string---    Private Function RemoveSpecialChars(ByVal str As String) As String        Dim NewString As String = String.Empty        Dim reg As Regex = New Regex("[A-Z]|[a-z]")        Dim coll As MatchCollection = reg.Matches(str)        For i As Integer = 0 To coll.Count - 1            NewString = NewString + coll(i).Value        Next        Return NewString    End Function

Next, define the SubscribeFeed() function to subscribe to a feed and then add each individual post to the TreeView control (see Figure 5).

Next, for each node representing a feed, the Text property is set to the feed’s title and its URL is stored in the Tag property of the node. For each node representing a posting, the Text property is set to the posting’s title and its description is stored in the Tag property (see Listing 3).

Wiring Up all the Event Handlers
With the helper functions defined, it’s time to wire up all the event handlers for the various controls.

First, code the Form1_Load event handler, as shown in Listing 4.

When the form is first loaded, you have to create a root node for the TreeView control and load all existing feeds. All subscribed feeds are saved in a plain text file, with the following format:

Feed URL|Feed URL|Feed URL|

An example would be:

http://news.google.com/?output=rss|http://rss.cnn.com/rss/cnn_topstories.rss|

In the Subscribe MenuItem control’s Click event handler, prompt the user to input a feed’s URL and then subscribe to the feed. If the subscription is successful, save the feed URL to file:

'---Subscribe MenuItem control---    Private Sub mnuSubscribe_Click( _       ByVal sender As System.Object, _       ByVal e As System.EventArgs) _       Handles mnuSubscribe.Click        Dim url As String = InputBox( _           "Please enter the feed URL", "Feed URL", "http://")        If url <> String.Empty Then            '---if feed is subscribed successfully---            If SubscribeFeed(url) Then                '---save in feed list---                Dim textwriter As TextWriter                textwriter = File.AppendText(FEEDS_LIST)                textwriter.Write(url & "|")                textwriter.Close()            Else                MsgBox("Feed not subscribed. " & _                "Please check that you have entered " & _                "the correct URL and that you have " & _                "Internet access.")            End If        End If    End Sub

Whenever a node in the TreeView control is selected, perform a check to see if it is a posting node and enable/disable the MenuItem controls appropriately (see also Figure 6).

    '---fired when each node is selected---    Private Sub TreeView1_AfterSelect( _       ByVal sender As System.Object, _       ByVal e As System.Windows.Forms.TreeViewEventArgs) _       Handles TreeView1.AfterSelect        If e.Node.ImageIndex <> ICO_POST And _           e.Node.Parent IsNot Nothing Then            mnuUnsubscribe.Enabled = True            mnuRefreshFeed.Enabled = True        Else            mnuUnsubscribe.Enabled = False            mnuRefreshFeed.Enabled = False        End If    End Sub

Figure 6. Everything’s Working: Enabling/disabling the MenuItem controls.
 
Figure 7. Viewing the Post: Using the WebBrowser control in Form2.

When the user selects a post using the Select button on the navigation pad, Form2 containing the WebBrowser control will be loaded and its content set accordingly (see Figure 7). This is handled by the KeyDown event handler:

    '---when the user presses the Select key on the keypad---    Private Sub TreeView1_KeyDown( _       ByVal sender As Object, _       ByVal e As System.Windows.Forms.KeyEventArgs) _       Handles TreeView1.KeyDown        Dim node As TreeNode = TreeView1.SelectedNode        If (e.KeyCode = System.Windows.Forms.Keys.Enter) Then            '---Enter key was pressed---            If node.ImageIndex = ICO_POST Then                Dim frm2 As New Form2                With frm2                    .Text = node.Text                    .WebBrowser1.DocumentText = node.Tag                    .Show()                End With            End If        End If    End Sub

To unsubscribe a feed, you will need to remove the feed’s URL from the text file and then remove the feed node from the TreeView control. This is handled by the Unsubscribe MenuItem control, shown in Listing 5.

When the user needs to refresh a feed, first make a backup copy of the feed XML document and proceed to subscribe to the same feed again. If the subscription is successful, remove the node containing the old feed. If the subscription is not successful (such as device is disconnected from the Internet, etc), restore the backup feed XML document. This is handled by the Refresh Feed MenuItem control, shown in Listing 6.

Finally, in the Back MenuItem control in Form2, code its Click event handler as follows:

    Private Sub mnuBack_Click( _       ByVal sender As System.Object, _       ByVal e As System.EventArgs) _       Handles mnuBack.Click        Me.Hide()    End Sub

That’s it! Press F5 to test the application. If you are testing the application using an emulator, you need to cradle the emulator to ActiveSync first so that you have Internet connectivity on the emulator. To cradle the emulator to ActiveSync, select Tools | Device Emulator Manager… in Visual Studio 2005 and right-click on the emulator you want to test and select Cradle.

Windows Mobile 6 Basics
Now you know how to:

  • Use the TreeView control to display information hierarchically
  • Manipulate files and read and write to text files
  • Download files using the WebRequest and WebResponse classes
  • Manipulate XML files using XPath expressions

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist