Android Tips: 寻找当前的Activity

来源:互联网 发布:猪八戒众包平台数据 编辑:程序博客网 时间:2024/06/06 04:26

通过如下的代码即可知道当前的活动Activity: 

public boolean isForeground(String PackageName){  // Get the Activity Manager  ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);  // Get a list of running tasks, we are only interested in the last one,   // the top most so we give a 1 as parameter so we only get the topmost.  List< ActivityManager.RunningTaskInfo > task = manager.getRunningTasks(1);   // Get the info we need for comparison.  ComponentName componentInfo = task.get(0).topActivity;  // Check if it matches our package name.  if(componentInfo.getPackageName().equals(PackageName)) return true;  // If not then our app is not on the foreground.  return false;}
需要权限 android.permission.GET_TASKS


0 0