gpt4 book ai didi

android - 为什么我们需要一个 Intent 来创建一个 Activity ?

转载 作者:行者123 更新时间:2023-11-29 15:23:38 24 4
gpt4 key购买 nike

我是 android 的新手,我无法在 android 中找到 Intent 的用途。

我能看懂代码:

   Intent i = new Intent(getApplicationContext(),Myclass.this);
startActivity(i);

它的作用。但我的问题是为什么我们需要 Intent 来创建一个 Activity。为什么 Android 用户不能允许我们直接创建 Activity 而不是使用 Intent

最佳答案

这更像是一个 Android 设计问题。背后的想法是,您总是只填充一个intent(ion)“我想查看此 URL”“我想开始我的主屏幕启动器”。系统会检查哪些应用能够满足此请求,如果有多种可能性,系统会允许您选择其中之一。

从某种角度来看,这更灵活一些。例如。在我上面的第一个例子中,你不需要知道 android 标准浏览器是否存在或者是否安装了 Chrome。您只需要求系统查看一个 URL:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(intent);

虽然有一些好处,但另一方面它可能看起来有点复杂。

关于android - 为什么我们需要一个 Intent 来创建一个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15460824/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com