public boolean isTopActivy(String cmdName){
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
        String cmpNameTemp = null;
 
        if(null != runningTaskInfos){
                cmpNameTemp=(runningTaskInfos.get(0).topActivity.getClassName()).toString();
                System.out.println(cmpNameTemp);
                Log.e("cmpname","cmpname:"+cmdName);
        }
 
       if(null == cmpNameTemp)return false;
       return cmpNameTemp.equals(cmdName);
}

对于原来代码修正了错误,详见下面链接

参考链接:1