Android中的Intent是什么? 有人能举个例子吗? intent的类型是什么,我们为什么要使用它们? 为什么intent在Android中如此重要?
当前回答
从广义上看,我们可以将Intent定义为
当一个Activity想要启动另一个Activity时,它会创建一个Object Intent指定它想要启动哪个Activity。
其他回答
什么是意图?
它是传递给组件的一种消息或信息。它被用来启动一个活动,显示一个网页,发送短信,发送电子邮件等。
在android中有两种类型的intent:
隐式意图 明确的意图
隐式意图用于调用系统组件
例子
Intent i = newIntent(android.content.Intent.ACTION_VIEW,Uri.parse(“http://www.amazon.com”));
startActivity(i);
显式意图用于调用活动类。
例子
Intent Intent = newIntent (this, SecondActivity.class);
startActivity(intent);
你可以阅读更多
http://www.vogella.com/tutorials/AndroidIntent/article.html#intents_overview http://developer.android.com/reference/android/content/Intent.html
意图是告诉Android你想做什么的一种方式。 换句话说,你要描述你的意图。意图可以用来向Android系统发出某个事件已经发生的信号。Android中的其他组件可以通过意图过滤器注册到这个事件。
以下是两种类型的意图
1.明确的意图
用于调用特定的组件。当您知道要启动哪个组件,但又不想让用户自由控制使用哪个组件时。例如,您有一个具有2个活动的应用程序。你想从活动A启动活动B。在这种情况下,你定义了一个明确的意图,目标是activityB,然后使用它直接调用它。
2.隐式意图
used when you have an idea of what you want to do, but you do not know which component should be launched. Or if you want to give the user an option to choose between a list of components to use. If these Intents are send to the Android system it searches for all components which are registered for the specific action and the data type. If only one component is found, Android starts the component directly. For example, you have an application that uses the camera to take photos. One of the features of your application is that you give the user the possibility to send the photos he has taken. You do not know what kind of application the user has that can send photos, and you also want to give the user an option to choose which external application to use if he has more than one. In this case you would not use an explicit intent. Instead you should use an implicit intent that has its action set to ACTION_SEND and its data extra set to the URI of the photo.
明确的意图总是传递给它的目标,无论它包含什么;没有参考过滤器。但是,只有当隐式意图能够通过组件的一个过滤器时,它才会被传递给组件
意图过滤器
如果一个intent被发送到Android系统,它将为这个intent确定合适的应用程序。如果已经为这种类型的intent注册了几个组件,Android会为用户提供打开其中一个的选择。
This determination is based on IntentFilters. An IntentFilters specifies the types of Intent that an activity, service, orBroadcast Receiver can respond to. An Intent Filter declares the capabilities of a component. It specifies what anactivity or service can do and what types of broadcasts a Receiver can handle. It allows the corresponding component to receive Intents of the declared type. IntentFilters are typically defined via the AndroidManifest.xml file. For BroadcastReceiver it is also possible to define them in coding. An IntentFilters is defined by its category, action and data filters. It can also contain additional metadata.
如果一个组件没有定义Intent过滤器,它只能被显式Intent调用。
下面是定义过滤器的两种方法
1.清单文件
如果你在清单中定义了意图过滤器,你的应用程序不需要运行来响应在它的过滤器中定义的意图。Android在安装应用程序时注册过滤器。
2.广播接收器
如果您希望广播接收器仅在应用程序运行时接收意图。然后,您应该在运行时(以编程方式)定义您的意图过滤器。请记住,这只适用于广播接收器。
根据他们的文件:
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平台允许通过意图接口进行转换。
使用意图演示,我建议你通过这个例子,因为他们也提供了一个代码文件。所以你可以使用它,也很容易理解。
Intent是执行某个动作的“意图”;换句话说,
可用于从另一个应用程序组件请求操作的消息传递对象
一个意图基本上是一个信息,说你已经或想要某事发生。根据意图,应用程序或操作系统可能会监听它,并做出相应的反应。你可以把它当成是发给一群朋友的电子邮件,告诉你的朋友John去做某事,或者告诉那些可以做X(“意图过滤器”)的朋友去做X。其他人会忽略这封邮件,但是John(或那些可以做X的朋友)会对它做出反应。
为了监听广播意图(如电话铃声或收到短信),您实现了一个广播接收器,它将被传递意图。为了声明你可以处理另一个应用程序的意图,比如“拍照”,你在应用程序的manifest文件中声明一个意图过滤器。
如果你想发射一个意图去做某事,比如弹出拨号器,你发射一个意图说你会。
推荐文章
- 警告: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文件