Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Twitter feed reader

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-01-2011, 09:26 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Twitter feed reader

(Requires Basic4android v1.2 or above)
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):
Code:
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:
Code:
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.
Code:
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
Code:
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.
Attached Files
File Type: zip TwitterDemo.zip (40.5 KB, 536 views)
File Type: apk Twitter.apk (122.9 KB, 123 views)
Reply With Quote
  #2 (permalink)  
Old 06-22-2011, 05:06 PM
Newbie
 
Join Date: Jun 2011
Posts: 3
Default 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
Reply With Quote
  #3 (permalink)  
Old 06-22-2011, 05:38 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

When your phone is "deep" sleeping it can suspend all services and disconnect the network in order to preserve battery.
Reply With Quote
  #4 (permalink)  
Old 10-05-2011, 08:49 PM
Knows the basics
 
Join Date: Aug 2011
Posts: 52
Default 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 by bergapappa : 10-06-2011 at 05:52 AM.
Reply With Quote
  #5 (permalink)  
Old 10-06-2011, 06:51 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

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.
Reply With Quote
  #6 (permalink)  
Old 10-06-2011, 09:03 PM
Knows the basics
 
Join Date: Aug 2011
Posts: 52
Default 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.
Reply With Quote
  #7 (permalink)  
Old 10-07-2011, 08:23 AM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 6,072
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Quote:
Originally Posted by bergapappa View Post
But after this task, I want to do another task after a while. In other words StartServiceAt again
Look at the code for Sub Service_Start in the first post. It reschedules itself there.
__________________
Sorry, but I don't answer questions by PM or email.
Please post your queries in the forum.
Reply With Quote
  #8 (permalink)  
Old 10-09-2011, 09:02 PM
Knows the basics
 
Join Date: Aug 2011
Posts: 52
Thumbs up 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
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Twitter Library alwaysbusy Additional libraries and official updates 9 04-06-2012 04:28 PM
Follow us on Twitter Erel Basic4android Updates and Questions 3 12-10-2010 10:59 AM
Get Twitter updates. ExcludeReality Questions (Windows Mobile) 1 01-28-2010 05:06 AM
New RSS feed Erel Forum Discussion 0 06-27-2009 07:06 PM
rss feed stbi Forum Discussion 0 07-06-2007 12:08 PM


All times are GMT. The time now is 10:30 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0