0

Is there any way to handle when my android application goes into background and back?

I want to use notification service for a on-line game - I use a service, which shows an alert when something happens in the game. I want alerts to show only if my application is active (on the foreground), so I need to start my service when application goes foreground and stop it when application goes background.

Note that I cannot use Activity.OnPause/OnResume methods. I have many activities in my application, and if I'll handle OnPause/OnResume, it is possible in a moment, when a user swtches one activity to another, application will look like background, thorough it will be foreground actually

PVoLan
  • 1,041
  • 2
  • 12
  • 24
  • 1
    Already discussed here: http://stackoverflow.com/questions/7338457/get-onpause-onresume-like-events-at-application-task-level/7450304#7450304 – Pointer Null Sep 22 '11 at 09:36

3 Answers3

0

Note that I cannot use Activity.OnPause/OnResume methods. I have many activities in my application, and if I'll handle OnPause/OnResume, it is possible in a moment, when a user swtches one activity to another, application will look like background, thorough it will be foreground actually

Why don't you write a base class that extends Activity and enables or disables the service in these methods? After that extend all your activities from this base activity. All you have to do is call the superclass method if you override these methods in your activties, e.g. by calling super.onResume() inside onResume(), to make sure these get still called. If you don't override them, everything works directly.

  • I don't want enable/disable service every time user switches an activity. I want to recieve notifications from server, for example, every 30 seconds, but user can switch an activity every second – PVoLan Sep 22 '11 at 10:28
  • In this case your question was unclear. Quote: *"so I need to start my service when application goes foreground and stop it when application goes background"* - foreground is when a activity is visible. –  Sep 22 '11 at 11:29
  • No. My application contains _many_ Activities. When one of them becomes invisible, another comes to foreground. OnResume/OnPause are called, but _application_ state doesn't changes – PVoLan Sep 26 '11 at 21:45
0

Not a clean way of doing this that I know of but,

You could perhaps send an Intent to your Service onCreate() and onPause() with a unique identifier.

You Service can then start a timer (with a delay which will be longer than the difference between onPause and onCreate being called in each activity) which, if not notified of an onCreate() within this time will set the Activity as "Paused".

If you add this functionality in a parent class which extends Activity you can then pull this same functionality into every class by extending that it rather than Activity (with the contract that you must call super.onCreate() and super.onPause() in each respective method).

Graeme
  • 25,714
  • 24
  • 124
  • 186
0

Problem solved in a following way (C# code, mono for android)

class MyService : Service{
  OnSomethingHappened(){
    ActivityManager am = (ActivityManager) GetSystemService(ActivityService);
    if(am.RunningAppProcesses.Any((arg) => 
      arg.ProcessName == "myprocessname" &&
      arg.Importance == ActivityManager.RunningAppProcessInfo.ImportanceForeground
    )){
       Trace("FOREGROUND!!!!");
    }else{
       Trace("BACKGROUND!!!!");
    }
  }
}
PVoLan
  • 1,041
  • 2
  • 12
  • 24