Android Tutorial Twitter feed reader

Status
Not open for further replies.
This example no longer works as it is based on an old Twitter API.
An updated example is available here: http://www.b4x.com/forum/basic4andr...example-application-only-auth.html#post176404



Please first read the Services tutorial.

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

twitter_1.png


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:

twitter_2.png


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):
B4X:
Sub btnGo_Click
    If EditText1.Text.Length = 0 Then
        ToastMessageShow("Please enter your query.", True)
        Return
    End If
    RssReader.Query = EditText1.Text
    RssReader.shouldScheduleNextTask = chkAutomatic.Checked
    StartService(RssReader)    
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:
B4X:
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, "UTF8"))
    HC.Execute(req, 1)
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.
B4X:
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")
            Log(state)
        Else
            state.Initialize
        End If
    End If
    Activity.LoadLayout("1")
    '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("chkAutomatic", True)
And in Service_Create
B4X:
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("EditText1")
        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.
 

Attachments

  • TwitterDemo.zip
    40.5 KB · Views: 3,820
  • Twitter.apk
    122.9 KB · Views: 1,173

INALAMBRIK

Member
Licensed User
Longtime 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.

Regards,
RA
 

bergapappa

Member
Licensed User
Longtime 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.

P.
 
Last edited:

bergapappa

Member
Licensed User
Longtime 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.
 

bergapappa

Member
Licensed User
Longtime User
Tnx

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).

Regards

bergapappa
 

mrazamerchant

Member
Licensed User
Longtime User
It is only reading those posts which has a retweat, it is not giving me complete list of all tweets.
 
Status
Not open for further replies.
Top