Android Programming Tutorials

(Romina) #1
Listening To Your Friends

}

}

};

In onCreate(), add a statement to start up our background thread on


threadBody:


@Override
public void onCreate() {
super.onCreate();

new Thread(threadBody).start();
}

Finally, in onDestroy(), set the active flag to false. Once the background


thread wakes up from its current sleep cycle, it will see the false flag and


fall out of its loop, terminating its thread:


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

active.set(false);
}

Step #3: Poll Your Friends..................................................................


Now, we have a service that will do work in the background. We just need


to set up the work itself.


What we want to do is load our timeline (with our status and those from


our friends) every poll period and find out those that are new. To do this,


we need to create a Twitter object, use it to load the timeline, track the


already-seen status messages, and identify the new ones. Right now, let us


focus on loading the timeline.


First, add an import to winterwell.jtwitter.Twitter so we can create


Twitter objects. We will also need java.util.List in a moment, so add an


import for it as well.


179
Free download pdf