1

I know this question is not new to stackoverflow, however I am still confused. So please don't mark this question as a duplicate, help me on this!

My android app has many activities. I need to call a web service, when my app comes to foreground and need to call another web service, when my app switched to background.

My initial findings are:

My activity:

  protected void onPause() 
    {
        AppUtil.trackAppBackgroundStatus(this);
        super.onPause();
    }

    protected void onResume() 
    {
        super.onResume();
        AppUtil.trackAppForegroundStatus(this);
    }

My utility class:

public class AppUtil 
{
    public static void trackAppForegroundStatus(Context theContext)
    {
        SharedPreferences aSharedSettings = theContext.getSharedPreferences("MyAppPreferences", Context.MODE_PRIVATE);
        String aAppStatus = aSharedSettings.getString("appStatus", "");

        if(!aAppStatus.equals("foreground"))
        {       
            SharedPreferences.Editor aPrefEditor = aSharedSettings.edit();
            aPrefEditor.putString("appStatus", "foreground");
            aPrefEditor.commit();

            trackSession(theContext, "foreground");
        }
    }

    public static void trackAppBackgroundStatus(Context theContext)
    {
        SharedPreferences aSharedSettings = theContext.getSharedPreferences("MyAppPreferences", Context.MODE_PRIVATE);
        String aAppStatus = aSharedSettings.getString("appStatus", "");

        if(!aAppStatus.equals("background"))
        {   
            SharedPreferences.Editor aPrefEditor = aSharedSettings.edit();
            aPrefEditor.putString("appStatus", "background");
            aPrefEditor.commit();

            trackSession(theContext, "background");
        }
    }
}

The trackSession method will track my app's foreground and background status.

Drawbacks: As said above, my app has various activities. So consider I have Page_A and Page_B.

In Page_A, after the onCreate() method call, control goes to onResume() and tracks that my app is in foreground.. When I move to the next page (Page_B), onPause() method of Page_A is called and tracks that my app is switched to background.

I don't want to track this by every activities.. I need to track my app's background status only when my app goes to background (that is, only when user presses home button and my app is switched to background).

Can anyone please guide me. Basically, I need to call 2 web-services. One, when app comes to foreground and the second, when app switches to background.

If these calls cannot be made with respect to app, how can I update my above code to handle them.

Any help please.

Community
  • 1
  • 1

3 Answers3

0
@Override
protected void onUserLeaveHint() {
    // TODO Auto-generated method stub
    super.onUserLeaveHint();
    // application is on background
}

In the documentation of Acitivity.onUserLeaveHint() you can find the following

Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called on the activity being interrupted. In cases when it is invoked, this method is called right before the activity's onPause() callback.

stinepike
  • 54,068
  • 14
  • 92
  • 112
  • Thanks for the reply StinePike. I tried this.. Even during page change (that is from Page_A to Page_B), onUserLeaveHint() is called. During the page change, I think the control goes in the following order. (1) onUserLeaveHint().. (2) onPause().. (3) onStop().. As I said above, I need to track the background event only when app goes to background (but not during page change). Suggestions please.. –  Mar 13 '13 at 07:46
  • I think you can do one thing just an experimental suggestion. after onpause save state like you did before then at the end of onPause set a postdelayed runnable say for 1 second. at that runnable first check the applcation state that you are saving. If the application state is background then you know the total application is in background – stinepike Mar 13 '13 at 07:54
0

As you said,

  • getRunningTasks() will solve my issue. However read that, Google will probably reject an app that uses ActivityManager.getRunningTasks().

but have a look at docs

            ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
        List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
        for(int i = 0; i < procInfos.size(); i++){
            if(procInfos.get(i).processName.equals("com.your.package.name")) {
//              procInfos.get(i).IMPORTANCE_FOREGROUND 
                Toast.makeText(getApplicationContext(), "your app is running", Toast.LENGTH_LONG).show();
            }
        }
Mehul Joisar
  • 15,348
  • 6
  • 48
  • 57
  • Thanks Mehul Joisar. Can you please suggest me where should I put the above code to check? –  Mar 13 '13 at 09:12
0

If min API level is 14 then Application.registerActivityLifecycleCallbacks method will solve this task easily. Just count active activities in "onActivityPaused" and "onActivityResumed" methods by incrementing/decrementing some value. So non zero value will indicate that application is in foreground.

Ruslan Yanchyshyn
  • 2,774
  • 1
  • 24
  • 22