比较 Android 意图对象

发布于 2024-12-13 14:18:32 字数 1090 浏览 3 评论 0原文

我有 2 个 android 意图对象,可以将其保留为 URL,然后重新恢复为意图对象。我想知道比较任何 2 个意图对象以确保它们最终解析为具有相同参数等的相同活动的最有效方法是什么。使用 intent.filterEquals 可以做到这一点,但它确实不包括额外费用。

目前我的 equals 方法代码如下所示:

            Intent a = Intent.parseUri(this.intentUrl,
                    Intent.URI_INTENT_SCHEME);

            Intent b = Intent.parseUri(other.intentUrl,
                    Intent.URI_INTENT_SCHEME);
            if (a.filterEquals(b)) {
                if (a.getExtras() != null && b.getExtras() != null) {
                    for (String key : a.getExtras().keySet()) {
                        if (!b.getExtras().containsKey(key)) {
                            return false;
                        } else if (!a.getExtras().get(key)
                                .equals(b.getExtras().get(key))) {
                            return false;

                        }
                    }
                }
                // all of the extras are the same so return true
                return true;
            } else { return false; }

但是有更好/更干净的方法吗?

I have 2 android intent objects that can be persisted as URLs and then rehydrated back into intent objects. I'm wondering what is the most effective way to compare any 2 intent objects to ensure that they end up resolving to the same activity with the same parameters etc. Using intent.filterEquals does this, but it does not include the extras.

Currently my code for the equals method looks like this:

            Intent a = Intent.parseUri(this.intentUrl,
                    Intent.URI_INTENT_SCHEME);

            Intent b = Intent.parseUri(other.intentUrl,
                    Intent.URI_INTENT_SCHEME);
            if (a.filterEquals(b)) {
                if (a.getExtras() != null && b.getExtras() != null) {
                    for (String key : a.getExtras().keySet()) {
                        if (!b.getExtras().containsKey(key)) {
                            return false;
                        } else if (!a.getExtras().get(key)
                                .equals(b.getExtras().get(key))) {
                            return false;

                        }
                    }
                }
                // all of the extras are the same so return true
                return true;
            } else { return false; }

But is there a better/cleaner way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

£噩梦荏苒 2024-12-20 14:18:32

至少在概念上,这可能已经是最好的了。但是,我不认为您的算法涵盖了 b 具有 a 没有的密钥的情况。

我将获取两个 keySet() 值并对它们运行 equals(),以确认它们都具有相同的键。然后,迭代一个并对值对运行 equals()

That's probably as good as it gets, at least conceptually. However, I don't think your algorithm covers cases where b has a key that a does not.

I'd get both keySet() values and run an equals() on those, to confirm they both have the same keys. Then, iterate over one and run equals() on the value pair.

终难遇 2024-12-20 14:18:32

通过添加缺少的 return 语句以及比较 extras 中的数组的条件来改进 @aostiles 的答案:

private boolean intentsAreEqual (Intent a, Intent b)
    {
        if (a.filterEquals(b)) {
            if (a.getExtras() != null && b.getExtras() != null) {
                // check if the keysets are the same size
                if (a.getExtras().keySet().size() != b.getExtras().keySet().size()) return false;
                // compare all of a's extras to b
                for (String key : a.getExtras().keySet()) {
                    if (!b.getExtras().containsKey(key)) {
                        return false;
                    }
                    else if (a.getExtras().get(key).getClass().isArray() && b.getExtras().get(key).getClass().isArray()) {
                        if (!Arrays.equals((Object[]) a.getExtras().get(key), (Object[]) b.getExtras().get(key))) {
                            return false;
                        }
                    }
                    else if (!a.getExtras().get(key).equals(b.getExtras().get(key))) {
                        return false;
                    }
                }
                // compare all of b's extras to a
                for (String key : b.getExtras().keySet()) {
                    if (!a.getExtras().containsKey(key)) {
                        return false;
                    }
                    else if (b.getExtras().get(key).getClass().isArray() && a.getExtras().get(key).getClass().isArray()) {
                        if (!Arrays.equals((Object[]) b.getExtras().get(key), (Object[]) a.getExtras().get(key))) {
                            return false;
                        }
                    }
                    else if (!b.getExtras().get(key).equals(a.getExtras().get(key))) {
                        return false;
                    }
                }
                return true;
            }
            if (a.getExtras() == null && b.getExtras() == null)
            {
                return true;
            }
            // either a has extras and b doesn't or b has extras and a doesn't
            return false;
        }
        else
        {
            return false;
        }
    }

Improving upon @aostiles' answer by adding the missing return statement and also conditions to compare arrays in extras:

private boolean intentsAreEqual (Intent a, Intent b)
    {
        if (a.filterEquals(b)) {
            if (a.getExtras() != null && b.getExtras() != null) {
                // check if the keysets are the same size
                if (a.getExtras().keySet().size() != b.getExtras().keySet().size()) return false;
                // compare all of a's extras to b
                for (String key : a.getExtras().keySet()) {
                    if (!b.getExtras().containsKey(key)) {
                        return false;
                    }
                    else if (a.getExtras().get(key).getClass().isArray() && b.getExtras().get(key).getClass().isArray()) {
                        if (!Arrays.equals((Object[]) a.getExtras().get(key), (Object[]) b.getExtras().get(key))) {
                            return false;
                        }
                    }
                    else if (!a.getExtras().get(key).equals(b.getExtras().get(key))) {
                        return false;
                    }
                }
                // compare all of b's extras to a
                for (String key : b.getExtras().keySet()) {
                    if (!a.getExtras().containsKey(key)) {
                        return false;
                    }
                    else if (b.getExtras().get(key).getClass().isArray() && a.getExtras().get(key).getClass().isArray()) {
                        if (!Arrays.equals((Object[]) b.getExtras().get(key), (Object[]) a.getExtras().get(key))) {
                            return false;
                        }
                    }
                    else if (!b.getExtras().get(key).equals(a.getExtras().get(key))) {
                        return false;
                    }
                }
                return true;
            }
            if (a.getExtras() == null && b.getExtras() == null)
            {
                return true;
            }
            // either a has extras and b doesn't or b has extras and a doesn't
            return false;
        }
        else
        {
            return false;
        }
    }
无风消散 2024-12-20 14:18:32

这几乎是 CommonsWare 建议与 Ben 的代码相结合的实现,但也涵盖了 a 有额外内容而 b 没有或 b 的情况> 有额外内容,而 a 没有。

private boolean areEqual(Intent a, Intent b) {
    if (a.filterEquals(b)) {
        if (a.getExtras() != null && b.getExtras() != null) {
            // check if the keysets are the same size
            if (a.getExtras().keySet().size() != b.getExtras().keySet().size()) return false;
            // compare all of a's extras to b
            for (String key : a.getExtras().keySet()) {
                if (!b.getExtras().containsKey(key)) {
                    return false;
                } else if (!a.getExtras().get(key).equals(b.getExtras().get(key))) {
                    return false;
                }
            }
            // compare all of b's extras to a
            for (String key : b.getExtras().keySet()) {
                if (!a.getExtras().containsKey(key)) {
                    return false;
                } else if (!b.getExtras().get(key).equals(a.getExtras().get(key))) {
                    return false;
                }
            }
        }
        if (a.getExtras() == null && b.getExtras() == null) return true;
        // either a has extras and b doesn't or b has extras and a doesn't
        return false;
    } else {
        return false;
    }
}

This is pretty much an implementation of what CommonsWare suggested combined with Ben's code but also covers the case where either a has extras and b does not or b has extras and a does not.

private boolean areEqual(Intent a, Intent b) {
    if (a.filterEquals(b)) {
        if (a.getExtras() != null && b.getExtras() != null) {
            // check if the keysets are the same size
            if (a.getExtras().keySet().size() != b.getExtras().keySet().size()) return false;
            // compare all of a's extras to b
            for (String key : a.getExtras().keySet()) {
                if (!b.getExtras().containsKey(key)) {
                    return false;
                } else if (!a.getExtras().get(key).equals(b.getExtras().get(key))) {
                    return false;
                }
            }
            // compare all of b's extras to a
            for (String key : b.getExtras().keySet()) {
                if (!a.getExtras().containsKey(key)) {
                    return false;
                } else if (!b.getExtras().get(key).equals(a.getExtras().get(key))) {
                    return false;
                }
            }
        }
        if (a.getExtras() == null && b.getExtras() == null) return true;
        // either a has extras and b doesn't or b has extras and a doesn't
        return false;
    } else {
        return false;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文