例如,我在我的项目中有以下代码:
public class Utilities extends Application
{
private static int stateCounter;
public void onCreate()
{
super.onCreate();
stateCounter = 0;
}
/**
* @return true if application is on background
* */
public static boolean isApplicationOnBackground()
{
return stateCounter == 0;
}
//to be called on each Activity onStart()
public static void activityStarted()
{
stateCounter++;
}
//to be called on each Activity onStop()
public static void activityStopped()
{
stateCounter--;
}
}但在ios中检测状态很简单:
let state: UIApplicationState = UIApplication.sharedApplication().applicationState // or use let state = UIApplication.sharedApplication().applicationState
if state == .Background {
// background
}
else if state == .Active {
// foreground
}我正在寻找android是否有其他解决方案来做这个测试?
发布于 2017-05-04 18:07:50
在应用程序类中编写此方法,并随心所欲地使用。
public boolean isApplicationBroughtToBackground() {
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(getPackageName())) {
return true;
}
}
return false;
}创建一个新类baseActivity
public class BaseActivity extends AppCompatActivity {
static Boolean IsResumecalled = false;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
super.onResume();
if (!IsResumecalled) {
//call api here
IsResumecalled= true;
}
}
@Override
protected void onStop() {
super.onStop();
if (Myapplication.getInstance().isApplicationBroughtToBackground()) {
//call api here
IsResumecalled= false;
}
}}
并使用BaseActivity为每个活动类进行扩展
发布于 2017-05-04 18:09:06
private boolean isAppOnForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = context.getPackageName();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
Log.d(TAG, "isAppOnForeground: " + appProcess.processName);
if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}
}发布于 2020-02-26 06:02:44
尝试使用Lifecycle的currentState方法。
public static boolean appIsOrWasInBackground() {
Lifecycle.State currentState = ProcessLifecycleOwner.get().getLifecycle().getCurrentState();
return currentState == Lifecycle.State.CREATED || currentState == Lifecycle.State.STARTED; // CREATED is when the app is currently in the background. STARTED is when the app is coming from the background, for example, from a `startActivity` call from a background Service or BroadcastReceiver. Either of these mean it _is_ or _was_ in the background (and should probably stay there).
}https://stackoverflow.com/questions/43779809
复制相似问题