我是 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/