提问者:小点点

Android UserManager.isUserAgoat()的正确用例?


我正在查看Android4.2中引入的新API。 在查看UserManager类时,我遇到了以下方法:

public boolean isUserAGoat()

用于确定发出此呼叫的用户是否受到远程传送的影响。

返回进行此调用的用户是否为GOAT。

这应该如何使用,何时使用?


共3个答案

匿名用户

从它们的源,用于返回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;
}

看起来该方法对我们这些开发人员来说没有真正的用处。 之前有人说这可能是个复活节彩蛋。

在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");
}

这里是来源和变化。

匿名用户

我不知道这是否是“正式的”用例,但以下内容在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;)。

我将while替换为if,产生了一个更模糊的用例。 我相信您可以使用这种行为来破坏您的IDE(比如Eclipse),但是这个编辑已经是4年之后的事情了,而且我还没有一个Eclipse环境可供使用。

匿名用户

这似乎是谷歌内部的一个笑话。 谷歌Chrome任务管理器中也有它的功能。 除了一些工程师觉得有趣之外,它没有任何目的。 这本身就是一个目的,如果你愿意的话。

  1. 在Chrome中,使用shift+esc打开任务管理器。
  2. 右键单击可添加Goats Teleport列。
  3. 疑惑。

甚至有一个巨大的铬虫报告关于太多的传送山羊。

下面的Chromium源代码片段是从HN注释中偷来的。

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