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

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

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


当前回答

这不是内部笑话

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

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

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

其他回答

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

这是来源和变化。

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

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

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

这不是内部笑话

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

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

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

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

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

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

从API 21(第一个Android 5.0/棒棒糖SDK)开始,这将检测山羊模拟器应用程序是否已安装:

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

这应该清楚地表明,如果(错误),djechlin将其用作无警告的建议是一种潜在的灾难性策略。以前为每个设备返回的错误现在返回了一个看似随机的值:如果这个值在代码中埋得足够深,那么可能需要很长时间才能弄清楚新的bug来自何处。

一句话:如果你不控制一个方法的实现,并决定将其用于API文档中所述以外的目的,那么你就会遇到麻烦。