0

How to know whether an application(not activity) running in background or not???

I am developing an alarm application. There are three activities A,B,C. A is the main app screen. User moves B from A. When B comes to foreground the alarm is registered. When B is finished the alarm is removed. When the receiver receives the notification from AlarmManager it invokes the Activity C. In C user can snooze stop the alarm.

My problem is when my application go to background, it should not ring the alarm. I want to remove the registered alarm. I can't override the B's onPause to remove the registered alarm because when the receiver receives the notification from alarm manager it invokes C to foreground and B moves to background. If i un-register the alarm in onPause of B, it will not ring in this scenario.

How to know the application is running in background (none of activities are in foreground) and then prevent invoking the C.

Thanks in advance

ADD
  • 195
  • 1
  • 1
  • 6
  • 1
    You can use boolean 0 or 1. When an activity starts set it to 1 and pass it to next activity. In next activity check what is the boolean value, if its 1, its still running. Else start it. – Rashmi.B Mar 05 '12 at 08:31

2 Answers2

0

When you application is running, its state will be available in ActivityManager.RunningAppProcessInfo. This class contains the processes currently running in the system. The process name is nothing but the package name of the application. See this answer for more details.

Community
  • 1
  • 1
AndroDev
  • 3,236
  • 8
  • 35
  • 49
  • Did it help you or not? Let us know whether you got your solution. – AndroDev Mar 05 '12 at 09:26
  • i tried the code in the link, but the check always returns true. i called the check in my alarm receiver. Scenario is like ...!). set the alarm. Then goto the activity B which register the alarm. Then press home button on mobile to send the app to background. When the alarm receiver receives the broadcast it checks for the RunningAppProcInfo. But there is showed the app is running forground – ADD Mar 05 '12 at 11:39
0

You should un-register alarm on activity B's onStop method it will be called when your activity B is destroyed alternatively you can unregister alarm on onDestroy of activity B as it is called when activity B is destroyed.

AndroidDev
  • 2,627
  • 6
  • 29
  • 41
  • onDestry is called when the Activity is destroyed, Is in't it? I want to unregister the alarm when user press Home button on mobile. I hope onPause is called when the Home button is pressed. But i can't use the onPause because when alarm receiver receives the broadcast message from AlarmManager it invokes another activity which pushes the activty B to background. – ADD Mar 05 '12 at 11:53