检查Android应用程序是在前台还是不?是在、前台、应用程序、Android

由网友(靠萌活着)分享简介:我经历了很多答案,这样去question.But这是所有关于单activity..How检查是否整个应用程序运行在前台还是不?I went through a lot of answers for this question.But it's all about single activity..How to che...

我经历了很多答案,这样去question.But这是所有关于单activity..How检查是否整个应用程序运行在前台还是不?

I went through a lot of answers for this question.But it's all about single activity..How to check whether the whole app is running in foreground or not ?

推荐答案

我不明白你想要什么,但你可以检测与 ActivityManager.getRunningAppProcesses目前前台/后台应用程序()电话。

I don't understand what you want, but You can detect currently foreground/background application with ActivityManager.getRunningAppProcesses() call.

喜欢的东西,

class ForegroundCheckTask extends AsyncTask<Context, Void, Boolean> {

  @Override
  protected Boolean doInBackground(Context... params) {
    final Context context = params[0].getApplicationContext();
    return isAppOnForeground(context);
  }

  private boolean isAppOnForeground(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null) {
      return false;
    }
    final String packageName = context.getPackageName();
    for (RunningAppProcessInfo appProcess : appProcesses) {
      if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
        return true;
      }
    }
    return false;
  }
}

// Use like this:
boolean foregroud = new ForegroundCheckTask().execute(context).get();

也让我知道,如果我误解了。

Also let me know if I misunderstand..

更新:看看这太问题Determining从后台任务或服务当前前台应用程序脱颖而出的详细信息。

UPDATE: Look at this SO question Determining the current foreground application from a background task or service fore more information..

谢谢..

阅读全文

相关推荐

最新文章