问题

我正在研究在Android 4.2中引入的新API。在查看UserManager类时,我遇到了以下方法:

public boolean isUserAGoat()

用于确定发起此呼叫的用户是否需要进行远程传送。返回进行此调用的用户是否是山羊。

它该如何使用和何时使用?


#1 热门回答(1490 赞)

从他们的source,用于返回false的方法直到它在API 21中被更改。

/**
 * Used to determine whether the user making this call is subject to
 * teleportations.
 * @return whether the user making this call is a goat 
 */
public boolean isUserAGoat() {
    return false;
}

看起来这个方法对我们来说并不是真正的开发者。之前有人表示可能是Easter egg

在API 21中,实现被更改为检查是否存在已安装的应用程序,其中包含“com.coffeestainstudios.goatsimulator”

/**
 * Used to determine whether the user making this call is subject to
 * teleportations.
 *
 * <p>As of {@link android.os.Build.VERSION_CODES#LOLLIPOP}, this method can
 * now automatically identify goats using advanced goat recognition technology.</p>
 *
 * @return Returns true if the user making this call is a goat.
 */
public boolean isUserAGoat() {
    return mContext.getPackageManager()
            .isPackageAvailable("com.coffeestainstudios.goatsimulator");
}

这里是source link


#2 热门回答(916 赞)

我不知道这是否是“官方用例”,但是下面的代码会在Java中产生一个警告(如果与return语句混合在一起,会导致编译错误,从而导致无法访问的代码):

while (1 == 2) { // Note that "if" is treated differently
    System.out.println("Unreachable code");
}

然而这是合法的:

while (isUserAGoat()) {
    System.out.println("Unreachable but determined at runtime, not at compile time");
}

所以我经常发现自己编写了一个愚蠢的实用方法,以最快的方式去伪造一个代码块,然后在完成调试时找到对它的所有调用,所以假如实现没有改变,那么可以使用它。

JLS指出if(false)不会触发“无法访问的代码”,因为这会打破对调试标志的支持,即基本上是这个用例(h / t @auselen)。 (例如``static final boolean DEBUG = false;`)。

我用'if'代替`while',产生一个更隐晦的用例。 Ibelieveyou可以通过这种行为将IDE与Eclipse一起绊倒,但是这种编辑是未来4年,我没有Eclipse环境可以使用。


#3 热门回答(706 赞)

这似乎是Google的一个笑话。它也在Google Chrome任务管理器中显示。除了一些工程师发现它有趣之外,它没有任何目的。如果你愿意,这本身就是一个目的。

  • 在Chrome中,使用Shift Esc打开任务管理器。
  • 右键单击​​以添加Goats Teleported列。
  • 奇迹。

甚至有一个关于too many teleported goats的巨大Chromium报告。

chrome

以下Chromium source code snippetHN评论中被盗。

int TaskManagerModel::GetGoatsTeleported(int index) const {
  int seed = goat_salt_ * (index + 1);
  return (seed >> 16) & 255;
}

原文链接