Android Activity Foreground Surveillance

In order to check if an Activity of my Android application is currently in the foreground I wrote a simple Service.
public class UEService extends Service {
private static final String TAG = "UEService";
private Timer timer;
private static final int delay = 1000; // delay for 1 sec before first start
private static final int period = 10000; // repeat check every 10 sec.

// This is the old onStart method that will be called on the pre-2.0
// platform. On 2.0 or later we override onStartCommand() so this
// method will not be called.
public void onStart(Intent intent, int startId) {

// To make this Service work in pre Level 5 APIs just remove this method
public int onStartCommand(Intent intent, int flags, int startId) {

// handles a Start command
private void handleCommand(Intent intent) {
Log.d(TAG, "service is starting");

if (timer == null) {
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
}, delay, period);

protected void checkActivityForeground() {
Log.d(TAG, "start checking for Activity in foreground");
Intent intent = new Intent();
sendOrderedBroadcast(intent, null, new BroadcastReceiver() {

public void onReceive(Context context, Intent intent) {
int result = getResultCode();

if (result != Activity.RESULT_CANCELED) { // Activity caught it
Log.d(TAG, "An activity caught the broadcast, result " + result);
Log.d(TAG, "No activity did catch the broadcast.");
}, null, Activity.RESULT_CANCELED, null, null);

protected void activityInForeground() {
Log.d(TAG, "starting method which gets called when an SureveillanceActivity is in foreground");

// TODO something you want to happen when an Activity is in the foreground

protected void noActivityInForeground() {
Log.d(TAG, "starting method which gets called when no SureveillanceActivity is in foreground");

// TODO something you want to happen when no Activity is in the foreground

stopSelf(); // quit

public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;

public void onDestroy() {
Log.d(TAG, "service is destroyed");
The Service class sends a special Intent witch gets answered by our custom Activity in case it is currently in the foreground.
public class UESurveillanceActivity extends Activity {
public static final String UE_ACTION = "at.mannaz.surveilance.inforeground";
private IntentFilter mIntentFilter;

private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(UE_ACTION)) {
Log.d("UESurveilanceActivity", "i'm in the foreground");

public void onCreate(Bundle savedInstanceState) {

mIntentFilter = new IntentFilter();

Intent serviceIntent = new Intent(this, UEService.class);

protected void onResume() {
registerReceiver(mIntentReceiver, mIntentFilter);


protected void onPause() {


This activity registers a BroadcastReceiver when it comes to the foreground and unregisters it when it goes to the background.

Here is how you use those two classes in your application:

public class DashboardActivity extends UESurveillanceActivity {

Thats all. All you have to do is to change the parent class of your Activity to UESurveillanceActivity and register the Service in your AndroidManifest.xml :

<service android:name=".UEService"></service>