0

I have looked around, but all suggestions are for checking state of my own app/activity. using the Life Cycle. I need to check if another app is in foreground/ running in background / paused . Is it possible. Based on the state of the app i need to change my further actions.

Deepak
  • 1,238
  • 3
  • 22
  • 47
  • http://developer.android.com/reference/android/app/ActivityManager.html – Ali Imran Dec 16 '12 at 07:12
  • may i know why you want to do this? what action you will do? – kumar_android Dec 16 '12 at 08:19
  • @kumaand i have a broadcast receiver that tracks incomming sms, and makes updates to db. If the app is in foreground i also need to update GUI for the app. – Deepak Dec 16 '12 at 08:32
  • Send Notification from broadcast receiver, Send the pending intent from notification and update the UI – kumar_android Dec 16 '12 at 13:19
  • @kumaand That will only make the user to call update, i found a diff method from the suggestion of Ali Imran above.I posting it as an answer now. – Deepak Dec 17 '12 at 03:54

1 Answers1

1
ActivityManager acm=(ActivityManager) getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > taskInfo = acm.getRunningTasks(1); 
taskInfo.get(0).topActivity.getClassName();
ComponentName componentInfo = taskInfo.get(0).topActivity;
String packageName=componentInfo.getPackageName();

I used Activity manager to find the top package / activity if this matches the requrements i send a broadcast to the app that will update the info from the db. This can simply be done by registering a broadcast receiver using code in the onResume and Unregister it in OnPause. So checking the Top package has just become a double check.

Deepak
  • 1,238
  • 3
  • 22
  • 47
  • Thank you , I just added a for loop in my service and checked for all running apps , if the package name exists in the list , then the app is running , I used this to display notification if the app is closed or floating window if the app is running.... Thanks again – Muhannad A.Alhariri Nov 07 '14 at 20:11