Android Programming Tutorials

(Romina) #1
Your Friends Seem Remote

package apt.tutorial.three;


import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import winterwell.jtwitter.Twitter;
import apt.tutorial.IPostListener;
import apt.tutorial.IPostMonitor;


public class PostMonitor extends Service {
private static final int POLL_PERIOD= 60000 ;
private static final int INITIAL_POLL_PERIOD= 1000 ;
private int pollPeriod=INITIAL_POLL_PERIOD;
private AtomicBoolean active=new AtomicBoolean(true);
private Set seenStatus=new HashSet();
private Map<IPostListener, Account> accounts=
new ConcurrentHashMap<IPostListener, Account>();


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


new Thread(threadBody).start();
}


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


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


active.set(false);
}


private void poll(Account l) {
try {
Twitter client=new Twitter(l.user, l.password);


client.setAPIRootUrl("https://identi.ca/api");


List<Twitter.Status> timeline=client.getFriendsTimeline();


for (Twitter.Status s : timeline) {
if (!seenStatus.contains(s.id)) {


215
Free download pdf