Android中的Intent是什么? 有人能举个例子吗? intent的类型是什么,我们为什么要使用它们? 为什么intent在Android中如此重要?
当前回答
一个Android应用程序可以包含零个或多个活动。当应用程序有多个活动时,通常需要从一个活动导航到另一个活动。在Android中,你通过所谓的意图在活动之间导航。你可以通过使用putExtra()将一些数据传递给你想要通过intent启动的活动。
其他回答
什么是意图?
Intent基本上是在组件(如活动、服务、广播接收器和内容提供者)之间传递的消息。因此,它几乎等同于传递给API调用的参数。API调用和通过意图调用组件之间的基本区别是:
API调用是同步的,而基于意图的调用是同步的 异步的。 API调用是编译时绑定,而基于意图的调用是 运行时绑定。
当然,通过使用所谓的显式意图(explicit intent),可以使intent完全像API调用一样工作,这将在后面解释。但通常情况下,隐式意图是可行的,这就是这里所解释的。
想要调用另一个组件的组件必须只表达其执行任务的意图。而任何其他存在的组件,并声称它可以通过意图过滤器完成这样的工作,由Android平台调用来完成这项工作。这意味着,两个组件都不知道彼此的存在,但仍然可以一起工作,为最终用户提供所需的结果。
这种组件之间的隐形连接是通过意图、意图过滤器和Android平台的组合来实现的。
这就带来了巨大的可能性,比如:
在运行时混合和匹配或者更确切地说即插即用组件。 用自定义开发的应用程序替换内置的Android应用程序 应用程序。 应用程序内部和应用程序之间的组件级重用。 如果我可以这么说的话,面向服务到最细粒度的级别。
以下是Android文档中关于intent的额外技术细节。
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a Background Service. An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed. The primary pieces of information in an intent are: action The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc. data The data to operate on, such as a person record in the contacts database, expressed as a Uri.
了解更多
Lars Vogel的教程 ProgrammerGuru文章 普遍的意图
Intent是一个类,用来绑定执行某些操作的信息。
示例:当用户在活动中执行此类操作时,将数据从一个活动传递给另一个活动
目前的活动。
根据他们的文件:
Intent是一个在独立组件(比如两个活动)之间提供运行时绑定的对象。Intent表示应用程序的“做某事的意图”。你可以在各种各样的任务中使用intent,但最常见的是它们被用来启动另一个活动。
下面是示例链接: http://developer.android.com/training/basics/firstapp/starting-activity.html#BuildIntent
正如文档所描述的,为了启动一个活动(你还需要理解什么是活动),使用如下的意图
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
意图基本上是一种将数据从一个活动传递到另一个活动的方式
在编写单个活动之后,需要转换到另一个活动来执行另一项任务,使用或不使用来自第一个活动的信息。
Android平台允许通过意图接口进行转换。
使用意图演示,我建议你通过这个例子,因为他们也提供了一个代码文件。所以你可以使用它,也很容易理解。
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件