需要跟踪我的应用程序的前景和后台状态

时间:2013-03-12 13:21:25

标签: android android-intent

我知道这个问题并不是stackoverflow的新问题,但我仍然感到困惑。所以请不要将此问题标记为重复,请帮助我!

我的Android应用程序有很多活动。当我的应用程序进入前台并需要调用其他Web服务时,我需要调用Web服务,当我的应用程序切换到后台时。

我最初的发现是:

我的活动:

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

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

我的实用程序类:

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");
        }
    }
}

trackSession方法将跟踪我的应用程序的前景和背景状态。

缺点: 如上所述,我的应用程序有各种活动。所以考虑一下我有Page_A和Page_B。

在Page_A中,在onCreate()方法调用之后,控制转到onResume()并跟踪我的应用程序在前台。当我移动到下一页(Page_B)时,调用Page_A的onPause()方法并且跟踪我的应用切换到后台。

我不希望每次活动都跟踪这个。我只有在我的应用程序转到后台时才会跟踪我的应用程序的后台状态(也就是说,只有当用户按下主页按钮并且我的应用程序切换到后台时)。

任何人都可以指导我。基本上,我需要拨打2个网络服务。一,当应用程序进入前台时,第二个,当应用程序切换到后台时。

如果无法针对应用进行这些调用,我该如何更新上述代码来处理它们。

请帮助。

3 个答案:

答案 0 :(得分:0)

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

在Acitivity.onUserLeaveHint()的文档中,您可以找到以下内容

  

当活动即将到来时,被称为活动生命周期的一部分   作为用户选择的结果进入后台。例如,何时   用户按下Home键,将调用onUserLeaveHint(),但是   当来电通话导致通话活动时   自动带到前台,onUserLeaveHint()不会   呼吁活动被打断。在调用它的情况下,   在活动的onPause()回调之前调用此方法。

答案 1 :(得分:0)

正如你所说,

  • getRunningTasks()将解决我的问题。但请注意,Google可能会拒绝使用ActivityManager.getRunningTasks()。
  • 的应用

但请查看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();
            }
        }

答案 2 :(得分:0)

如果min API级别为14,则Application.registerActivityLifecycleCallbacks方法将轻松解决此任务。只需通过递增/递减某些值来计算“onActivityPaused”和“onActivityResumed”方法中的活动活动。所以非零值表示应用程序在前台。

相关问题