Android Programming Tutorials

(Romina) #1
Listening To Your Friends

}

@Override
public IBinder onBind(Intent intent) {
return(null);
}

@Override
public void onDestroy() {
super.onDestroy();
}
}

We also need to add our service to AndroidManifest.xml, so add <service


android:name = ".PostMonitor" /> inside the element.


At this point, your project will still compile and run, though our service is


not yet doing anything.


Step #2: Set Up a Background Thread...............................................


Next, we need a well-managed background thread that can do our timeline


polling for us.


First, add an import for java.util.concurrent.atomic.AtomicBoolean, along


with a private AtomicBoolean data member named active. Initialize this to


true.


Also, define a static int value named POLL_PERIOD to be the amount of time


you want between timeline polling operations. Please make this no less


than 60000 (one minute in milliseconds).


Then, create a Runnable named threadBody that will loop, sleeping


POLL_PERIOD each pass, until active is toggled to false:


private Runnable threadBody=new Runnable() {
public void run() {
while (active.get()) {
// do something with Twitter

SystemClock.sleep(POLL_PERIOD);

178
Free download pdf