Android Tutorial Twitter feed reader

Discussion in 'Tutorials & Examples' started by Erel, Feb 1, 2011.

Thread Status:
Not open for further replies.
  1. Erel

    Erel Administrator Staff Member Licensed User

    This example no longer works as it is based on an old Twitter API.
    An updated example is available here:

    Please first read the Services tutorial.

    The Twitter example reads the RSS feed of a specific query and shows the results:


    Clicking on an item that contains a link opens the link in the browser.

    If the "Update automatically" option is checked when you submit a query then the background service will issue this query every 3 minutes.

    Then if the activity is visible it will be updated, otherwise a notification will appear in the status bar to notify the user that there are new items.

    The most recent message appears when the user opens the notifications screen:


    The automatic updates continue till the user unchecks the Update automatically option.

    The code includes 3 modules: Main - the main activity, RSSReader - the service that downloads and parses the feed and SavedTwits code module which is responsible for holding the list of twits, saving it to a file and loading it from a file when needed.

    Starting the service is done when the user clicks on the Go button (or presses on the keyboard Done button):
    Sub btnGo_Click
    If EditText1.Text.Length = 0 Then
    ToastMessageShow("Please enter your query."True)
    End If
        RssReader.Query = EditText1.Text
        RssReader.shouldScheduleNextTask = chkAutomatic.Checked
    End Sub
    Before starting the service we set two process global variables. The query and whether the user wants to update automatically.

    The code in Service_Start is:
    Sub Service_Start
    'schedule the next run in 3 minutes
        If shouldScheduleNextTask Then
    StartServiceAt(""DateTime.Now + 3 * DateTime.TicksPerMinute, False)
    End If
    Dim req As HttpRequest
        req.InitializeGet(URL & su.EncodeUrl(Query, 
    End Sub
    First we check if we need to schedule another run in 3 minutes. This will actually cause the task to repeat itself every 3 minutes (as it will reschedule itself each time).
    Note that if the device is sleeping our task will wait till it wakes up.
    Then we submit the HttpRequest and asynchronously read the stream.

    It is very important to understand that at some point the whole process will be killed and then recreated when the time for the next task arrives.
    This means that each time our activity is paused we need to save the current state. Later when the activity is created or the service is created we can use the saved state to start correctly.
    A Map object together with File.ReadMap and File.WriteMap make it much easier to save and restore the state. The Map holds pairs of keys and values.

    When the activity starts we check if it is the first time it is created. First time means that it is the first time since the containing process has started.
    Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
    'load previous values if file exists
            If File.Exists(File.DirInternal, "state.txt"Then
                state = 
    File.ReadMap(File.DirInternal, "state.txt")
    End If
    End If
    'load the previous state or set the defaults if this is the first time.
        'GetDefault returns the stored value or the second parameter if the key was not found.
        EditText1.Text = state.GetDefault("EditText1""#android")
        chkAutomatic.Checked = state.GetDefault(
    And in Service_Create
    Sub Service_Create
    If Query = "" Then
    'no query means that the process was killed.
            'so we load the query from the state file saved by the activity.
            Dim state As Map
            state = 
    File.ReadMap(File.DirInternal, "state.txt")
            Query = state.Get(
    Log("Loading query from file.")
    End If
    The source code is included in the zip file.

    ...And if we are talking about Twitter, you are all welcomed to follow us on Twitter.

    Attached Files:

    desof likes this.

    INALAMBRIK Member Licensed User

    Twitter App Not Works when the Phone is in Sleep Mode

    I've download this app on my Samsung Galaxy ACE and test it, but when it goes to sleep mode the twitts doesn't refresh unless I interact with the phone.

  3. Erel

    Erel Administrator Staff Member Licensed User

    When your phone is "deep" sleeping it can suspend all services and disconnect the network in order to preserve battery.
  4. bergapappa

    bergapappa Member Licensed User

    Deep Sleep?

    I thought the phone went to sleep when the screen goes black.
    Is there a deeper sleep?

    I'm trying to keep a timer alive in a service module during sleep but android seems to kill it after a very short while.

    Last edited: Oct 6, 2011
  5. Erel

    Erel Administrator Staff Member Licensed User

    There are several sleeping modes.
    If you want to periodically run a task then you should use StartServiceAt. It runs even when the device is sleeping. You may need to acquire a power lock with PhoneWakeState during the task.
  6. bergapappa

    bergapappa Member Licensed User

    StartServiceAt again

    TNX Erel.

    Now when i got the StartServiceAt tips i could find it in the community.
    Sorry, just a newbie.

    My app is supposed preform a task after a certain time, so StartServiceAt fits perfect. But after this task, I want to do another task after a while. In other words StartServiceAt again, but from where.
    I mean, the user has not been involved.
    Do I need another service module so I have one for the task and one just for the purpose to start the other service?

    Or how would you solve this?

    short description
    (in the main activity the user clicks a button - StartServiceAt.
    the service preforms a task after say RND min. After this another task shall be done after a random amount of time and over again till the user wakes up the phone and clicks the button again.)

    TNX again
    Just love your software and the impressive amount of info in the forums.
  7. agraham

    agraham Expert Licensed User

    Look at the code for Sub Service_Start in the first post. It reschedules itself there.
  8. bergapappa

    bergapappa Member Licensed User


    TNX a lot, should have seen this.
    Wasn't looking for the reproduce from the beginning because I then used a timer.
    This works like a charm, TNX again.

    One thing I don't get though is if the service is running or not in between for example the first and the second StartServiceAt.

    The reason I'm asking is if I need to use both CancelScheduledService(ljudservice) and StopService(ljudservice) when I want to stop this flow (loop).


  9. mrazamerchant

    mrazamerchant Member Licensed User

    It is only reading those posts which has a retweat, it is not giving me complete list of all tweets.
  10. Erel

    Erel Administrator Staff Member Licensed User

  11. desof

    desof Well-Known Member Licensed User

    I downloaded the example and I get error: gone

  12. Erel

    Erel Administrator Staff Member Licensed User

    See the first sentence in the tutorial...
Thread Status:
Not open for further replies.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice