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

公共布尔值isUserAGoat()用于确定拨打此呼叫的用户是否需要进行远程传送。返回进行此调用的用户是否为山羊。

该如何使用以及何时使用?


当前回答

从可用性和可访问性的角度来看,了解您的用户是否是山羊很重要。

如果山羊想要真正受益于信息时代所提供的一切,那么它们的需求与软件的需求其实是截然不同的。

我觉得很好,一些好心的开发者关心我们所有的用户,而不仅仅是那些时尚的用户。

其他回答

谷歌非常喜欢山羊和山羊复活节彩蛋。之前甚至有关于它的Stack Overflow帖子。

如前几篇文章所述,它也存在于Chrome任务管理器中(2009年首次出现在野外):

<message name="IDS_TASK_MANAGER_GOATS_TELEPORTED_COLUMN" desc="The goats teleported column">
    Goats Teleported
</message>

然后在2010年初的Windows、Linux和Mac版本的Chrome中)。“Goats Teleported”的数量实际上是随机的:

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

谷歌对山羊的其他引用包括:

用山羊割草山羊是baaaahk

据我所知,山羊和谷歌最早的关联是在最初的“用山羊割草”博客文章中。

我们可以放心地假设,它只是一个复活节彩蛋,除了返回错误之外,没有任何实际用途。

Android R更新:

在Android R中,此方法始终返回false。谷歌表示,这样做是为了“保护山羊隐私”:

/**
 * 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>
 *
 * <p>As of {@link android.os.Build.VERSION_CODES#R}, this method always returns
 * {@code false} in order to protect goat privacy.</p>
 *
 * @return Returns whether the user making this call is a goat.
 */
public boolean isUserAGoat() {
    if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.R) {
        return false;
    }
    return mContext.getPackageManager()
            .isPackageAvailable("com.coffeestainstudios.goatsimulator");
}

上一个答案:

从他们的源代码来看,该方法在API 21中更改之前一直返回false。

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

这是来源和变化。

这不是内部笑话

显然它只是一个应用程序检查器山羊模拟器-咖啡色工作室

如果你安装了山羊模拟器,你就是一只山羊。如果你没有安装,你就不是山羊。

我想这更多是一个开发者的个人实验,最有可能找到有共同兴趣的人。

有一个类似的调用isUserAMonkey(),如果正在使用MonkeyRunner工具,则返回true。SDK的解释和这个一样奇怪。

公共静态布尔值isUserAMonkey(){}如果用户界面当前被猴子搞乱,则返回true。

这是来源。

我预计,这是在一个名为something with a goat的新SDK工具的预期中添加的,它将实际用于测试该工具的存在。

另请参阅一个类似的问题,ActivityManager:isUserAMonkey中的奇怪函数。这意味着什么,它的用途是什么?。

作为对@djechlin答案的补充(顺便说一句,这是一个好答案!),当您想在某个特定的迭代或特定的递归调用中停止时,此函数调用也可以用作虚拟代码,以在IDE中保存断点,例如:

可以使用isUserAGoat()代替虚拟变量声明,该声明将在IDE中显示为警告,并且在Eclipse的特定情况下,将阻塞断点标记,使其难以启用/禁用。如果该方法用作约定,则所有调用都可以稍后由某个脚本过滤(可能在提交阶段?)。

谷歌是重度Eclipse用户(他们提供了几个Eclipse插件项目:Android SDK、GAE等),所以@djechlin的回答和这个补充答案很有意义(至少对我来说)。