检查Android应用程序是在前台还是不? [英] check android application is in foreground or not?

查看:108
本文介绍了检查Android应用程序是在前台还是不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经历了很多答案,这样去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..

更新:看看这太问题<一href="http://stackoverflow.com/questions/2166961/determining-the-current-foreground-application-from-a-background-task-or-service">Determining从后台任务或服务当前前台应用程序脱颖而出的详细信息。

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

谢谢..

这篇关于检查Android应用程序是在前台还是不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆