Android Programming Tutorials

(Romina) #1
Now, Your Friends Are Alarmed

String parameter that is the "name" of the service. What role this name


plays is unclear. So, add a functional constructor:


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

Now you can get rid of the isActive and threadBody data members, along


with all references to them (which you will find in onCreate() and


onDestroy()).


Now, if you rebuild and reinstall the project, Patchy should work as before.


The exception is that if you have Patchy on a device, and you let the device


fall asleep while Patchy is still active, you should still receive status updates.


Here is the entire listing of PostMonitor after all of these changes:


package apt.tutorial.two;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Binder;
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;
import com.commonsware.cwac.wakeful.WakefulIntentService;

public class PostMonitor extends WakefulIntentService {
public static final int NOTIFICATION_ID= 1337 ;
public static final String STATUS_UPDATE="apt.tutorial.three.STATUS_UPDATE";

294
Free download pdf