Android Programming Tutorials

(Romina) #1
Now, Your Friends Are Alarmed

public static final String FRIEND="apt.tutorial.three.FRIEND";
public static final String STATUS="apt.tutorial.three.STATUS";
public static final String CREATED_AT="apt.tutorial.three.CREATED_AT";
public static final String POLL_ACTION="apt.tutorial.three.POLL_ACTION";
private static final String NOTIFY_KEYWORD="snicklefritz";
private static final int INITIAL_POLL_PERIOD= 1000 ;
private static final int POLL_PERIOD= 60000 ;
private Set seenStatus=new HashSet();
private Map<IPostListener, Account> accounts=
new ConcurrentHashMap<IPostListener, Account>();
private final Binder binder=new LocalBinder();
private AtomicBoolean isBatteryLow=new AtomicBoolean(false);
private AlarmManager alarm=null;
private PendingIntent pi=null;


public PostMonitor() {
super("PostMonitor");
}


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


registerReceiver(onBatteryChanged,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));


alarm=(AlarmManager)getSystemService(Context.ALARM_SERVICE);


Intent i=new Intent(this, OnAlarmReceiver.class);


pi=PendingIntent.getBroadcast(this, 0 , i, 0 );
setAlarm(INITIAL_POLL_PERIOD);
}


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


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


alarm.cancel(pi);
unregisterReceiver(onBatteryChanged);
}


@Override
protected void doWakefulWork(Intent i) {
if (i.getAction().equals(POLL_ACTION)) {
for (Account l : accounts.values()) {
poll(l);
}
}


295
Free download pdf