-2

i'm trying to build an application that every given time, will connect to a remote server and get JSON object from it.

as i searched the web for answer i wasn't able to realize exactly how to setup a service and run it as long as the application is running.

i want my main.xml screen have some kind of TextView which will update from the service.

couldn't found anywhere how to build a service which update a TextView when needed..

i'm looking for a simple example - as for i'm newbie to android development.

Asaf Nevo
  • 11,338
  • 23
  • 79
  • 154

2 Answers2

1

What you should look for -- perhaps instead of a service in this instance -- is an AsyncTask. This is what you use when you need to update the UI from the background and not hang around too long on the main thread. Here's one AsyncTask tutorial, and here's what the Android SDK docs have to say about it.

If you need to do things like download JSON every so often from a server, a Service might be a good solution. To communicate back and forth between a Service and an Activity you will use a Messenger and Handler example. You can find an example of how to use the messenger / handler pattern for services and activities in the API demos included with the SDK (here). this SO thread is also relevant.

If you need to keep your service running every so often, you'll want to look at using an AlarmManager to grab the data, store it somewhere, and then refresh the display in the Activity (perhaps through a database in your app). But basically, if you need to quickly download some stuff and update an Activity, use an AsyncTask, if you need something longer term, bind a service and then communicate back and forth between it and the Activity using a Messenger / Handler pair (or AIDL, but that's more complicated..)

Community
  • 1
  • 1
Kristopher Micinski
  • 7,572
  • 3
  • 29
  • 34
  • yes so i've read.. but my problem with the SDK examples that sometimes there're too complicated for me to understand.. can u perhaps guide me to a very simple tutorial which only show the basics of the process ? – Asaf Nevo Mar 21 '12 at 06:21
  • Oh, that's harder to do... It's a fairly complicated process, not a simple function call or anything. Instead maybe you should read about what remote procedure calls / inter process communication is before attempting to do something like this? – Kristopher Micinski Mar 21 '12 at 13:21
  • that's i'll do too of course :) isn't there a simple way which my service can setText() inside a TextView object? something like: TextView text = (TextView) findViewById(R.id.text1); ? – Asaf Nevo Mar 21 '12 at 17:16
  • No. Because the Service runs on a Thread that's not the UI thread. If you try to update the UI thread from a Service you will get an exception thrown at you. Instead of using a service, you can use an AsyncTask, which _will_ let you do this (that's what it was designed for), but if you're using a service, you have to send a message to the main thread that says something like 'hey, change the text now!' – Kristopher Micinski Mar 21 '12 at 17:23
0

You might be able to use a bound service to achieve it. http://developer.android.com/guide/topics/fundamentals/bound-services.html

Gautham
  • 3,418
  • 3
  • 30
  • 41