0

How to have two different location update interval in Fused location when app is in foreground (For example: 5 sec) and when app is in background (For example 5 minute)?

4

1 回答 1

0

您可以检查应用程序是否在前台。

private boolean isAppInForeground()
{
    ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> services = activityManager.getRunningTasks(1);

    return (services.get(0).topActivity.getPackageName()
            .equalsIgnoreCase(getApplicationContext().getPackageName()));
}

然后在清单中添加:

<uses-permission android:name="android.permission.GET_TASKS"/>
于 2015-05-28T18:11:16.627 回答