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 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.
- @Override
- public void onStart(Intent intent, int startId) {
- handleCommand(intent);
- }
-
- // To make this Service work in pre Level 5 APIs just remove this method
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- handleCommand(intent);
- return START_NOT_STICKY;
- }
-
- // handles a Start command
- private void handleCommand(Intent intent) {
- Log.d(TAG, "service is starting");
-
- if (timer == null) {
- public void run() {
- checkActivityForeground();
- }
- }, delay, period);
- }
- }
-
- protected void checkActivityForeground() {
- Log.d(TAG, "start checking for Activity in foreground");
- Intent intent = new Intent();
- intent.setAction(UESurveillanceActivity.UE_ACTION);
- sendOrderedBroadcast(intent, null, new BroadcastReceiver() {
-
- @Override
- int result = getResultCode();
-
- if (result != Activity.RESULT_CANCELED) { // Activity caught it
- Log.d(TAG, "An activity caught the broadcast, result " + result);
- activityInForeground();
- return;
- }
- Log.d(TAG, "No activity did catch the broadcast.");
- noActivityInForeground();
- }
- }, 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
- }
-
- @Override
- public IBinder onBind(Intent arg0) {
- // TODO Auto-generated method stub
- return null;
- }
-
- @Override
- public void onDestroy() {
- super.onDestroy();
- timer.cancel();
- 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 {
- private IntentFilter mIntentFilter;
-
- private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
- @Override
- if(action.equals(UE_ACTION)) {
- Log.d("UESurveilanceActivity","i'm in the foreground");
- this.setResultCode(Activity.RESULT_OK);
- }
- }
- };
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- mIntentFilter = new IntentFilter();
- mIntentFilter.addAction(UE_ACTION);
-
- Intent serviceIntent = new Intent(this, UEService.class);
- startService(serviceIntent);
- }
-
- @Override
- protected void onResume() {
- registerReceiver(mIntentReceiver, mIntentFilter);
-
- super.onResume();
- }
-
- @Override
- protected void onPause() {
- unregisterReceiver(mIntentReceiver);
-
- super.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 Dashboard 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 manifest:
<service android:name=".UEService"></service>
If you override onCreate, onStop or onStart in your Activity don't forget to call super.onCreate/Stop/Start trough.
Download
Here is an Eclipse example project with all the sourcecodes: ActivitySurveillance.zip
Comments:
Thanks in advance
Olivier