Android Intent的简介以及属性的详解

安卓实验3 Intent、Activity的使用 可以使用SharedPreferences的名称作为Extra放入Intent中,然后在接收Intent的另一个Activity中使用SharedPreferences的名称来获取SharedPreferences对象。用户在MainActivity中输入用户名和密码,点击登录按钮后,程序将检查用户名和密码是否正确,如果正确则跳转到DisplayActivity中显示用户名和密码。2)创建一个会员注册程序;第一个界面输入用户名,下一步之后,进入第二个界面输入用户的单位,从第二个界面返回后的得到用户的信息。 阅读详情

一.Intent的介绍

Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。Intent不仅可用于应用程序之间,也可用于应用程序内部的Activity/Service之间的交互。因此,可以将Intent理解为不同组件之间通信的“媒介”专门提供组件互相调用的相关信息。

二.Inten启动组件的方法

Intent可以启动一个Activity,也可以启动一个Service,还可以发起一个广播Broadcasts。具体方法如下:

组件名称

方法名称

 

Activity

startActvity( )

startActivity( )

 

Service

startService( )

bindService( )

 

Broadcasts

sendBroadcasts( )

sendOrderedBroadcasts( )

sendStickyBroadcasts( )

三.Intent的属性

Intent有以下几个属性:

动作(Action),数据(Data),分类(Category),类型(Type),组件(Compent)以及扩展信(Extra)。其中最常用的是Action属性和Data属性。

1.Intent的Action属性

Action是指Intent要完成的动作,是一个字符串常量。SDK中定义了一些标准的Action常量如下表所示。

Constant

Target component

Action

ACTION_CALL

activity

Initiate a phone call.

ACTION_EDIT

activity

Display data for the user to edit.

ACTION_MAIN

activity

Start up as the initial activity of a task, with no data input and no returned output.

ACTION_SYNC

activity

Synchronize data on a server with data on the mobile device.

ACTION_BATTERY_LOW

broadcast receiver

A warning that the battery is low.

ACTION_HEADSET_PLUG

broadcast receiver

A headset has been plugged into the device, or unplugged from it.

ACTION_SCREEN_ON

broadcast receiver

The screen has been turned on.

ACTION_TIMEZONE_CHANGED

broadcast receiver

The setting for the time zone has changed.

 下面是一个测试Action常量的例子:

main.xml

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7.     <TextView    
  8.         android:layout_width="fill_parent"   
  9.         android:layout_height="wrap_content"   
  10.         android:text="@string/hello" 
  11.         /> 
  12.     <Button   
  13.         android:text="测试Action属性" 
  14.         android:id="@+id/getBtn" 
  15.         android:layout_width="wrap_content"   
  16.         android:layout_height="wrap_content"   
  17.         /> 
  18. </LinearLayout> 

 strings.xml

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.     <string name="hello">测试Action属性</string> 
  4.     <string name="app_name">IntentActionDemo</string> 
  5. </resources> 

MainActivity.java

 
  1. package com.android.action.activity;  
  2.  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9.  
  10. public class MainActivity extends Activity {  
  11.     private Button getBtn;  
  12.     @Override 
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.           
  17.         getBtn=(Button)findViewById(R.id.getBtn);  
  18.         getBtn.setOnClickListener(new OnClickListener() {  
  19.             @Override 
  20.             public void onClick(View v) {     
  21.                 Intent intent = new Intent();                 
  22.                 intent.setAction(Intent.ACTION_GET_CONTENT);// 设置Intent Action属性                  
  23.                 intent.setType("vnd.android.cursor.item/phone");// 设置Intent Type 属性   
  24.                                                                 //主要是获取通讯录的内容  
  25.                 startActivity(intent); // 启动Activity  
  26.             }  
  27.         });          
  28.     }  
  29. } 

效果图:

2.Intent的Data属性

Intent的Data属性是执行动作的URI和MIME类型,不同的Action有不同的Data数据指定。比如:ACTION_EDIT Action应该和要编辑的文档URI Data匹配,ACTION_VIEW应用应该和要显示的URI匹配。

3.Intent的Category属性

Intent中的Category属性是一个执行动作Action的附加信息。比如:CATEGORY_HOME则表示放回到Home界面,ALTERNATIVE_CATEGORY表示当前的Intent是一系列的可选动作中的一个。下表是SDK文档中关于Category的信息。

Constant

Meaning

CATEGORY_BROWSABLE

The target activity can be safely invoked by the browser to display data referenced by a link — for example, an image or an e-mail message.

CATEGORY_GADGET

The activity can be embedded inside of another activity that hosts gadgets.

CATEGORY_HOME

The activity displays the home screen, the first screen the user sees when the device is turned on or when the HOME key is pressed.

CATEGORY_LAUNCHER

The activity can be the initial activity of a task and is listed in the top-level application launcher.

CATEGORY_PREFERENCE

The target activity is a preference panel.

 下面是一个回到Home界面的例子:

main.xml

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     >     
  6.     <TextView   
  7.     android:layout_width="fill_parent" 
  8.     android:layout_height="wrap_content"   
  9.     android:text="测试Intent Category"   
  10.     /> 
  11.     <Button   
  12.     android:id="@+id/Button1"   
  13.     android:layout_width="wrap_content" 
  14.     android:layout_height="wrap_content"   
  15.     android:text="转到Home界面" 
  16.     />    
  17. </LinearLayout> 

strings.xml

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.     <string name="hello">Hello World, MainActivity!</string> 
  4.     <string name="app_name">IntentCategoryDemo</string> 
  5. </resources> 

MainActivity.java

 
  1. package com.android.category.activity;  
  2.  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9.  
  10. public class MainActivity extends Activity {  
  11.     private Button btn;  
  12.     @Override 
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.           
  17.         btn = (Button)findViewById(R.id.Button1);  
  18.         btn.setOnClickListener(new OnClickListener() {  
  19.             @Override 
  20.             public void onClick(View v) {     
  21.                 Intent intent = new Intent();                 
  22.                 intent.setAction(Intent.ACTION_MAIN);// 添加Action属性                
  23.                 intent.addCategory(Intent.CATEGORY_HOME);// 添加Category属性              
  24.                 startActivity(intent);// 启动Activity  
  25.             }  
  26.         });  
  27.     }  
  28. } 

 效果图:

 

4.Intent的Type属性

Intent的Type属性显式指定Intent的数据类型(MIME)。一般Intent的数据类型能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导。
 

5.Intent的Compent属性

Intent的Compent属性指定Intent的的目标组件的类名称。通常 Android会根据Intent 中包含的其它属性的信息,比如action、data/type、category进行查找,最终找到一个与之匹配的目标组件。但是,如果 component这个属性有指定的话,将直接使用它指定的组件,而不再执行上述查找过程。指定了这个属性以后,Intent的其它所有属性都是可选的。

 6.Intent的Extra属性

Intent的Extra属性是添加一些组件的附加信息。比如,如果我们要通过一个Activity来发送一个Email,就可以通过Extra属性来添加subject和body。

 下面的例子在第一个Activity的EditText输入用户名,该年龄保存在Intent的Extras属性中。当单击Button时,会在第二个Activity中显示用户名。

first.xml

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical"   
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     >     
  7.     <TextView     
  8.         android:layout_width="wrap_content" 
  9.         android:layout_height="wrap_content"   
  10.         android:text="请输入用户名"   
  11.         />        
  12.     <EditText   
  13.         android:id="@+id/EditText1"   
  14.         android:layout_width="fill_parent" 
  15.         android:layout_height="wrap_content" 
  16.         />        
  17.     <Button   
  18.         android:id="@+id/Button1"   
  19.         android:layout_width="wrap_content" 
  20.         android:layout_height="wrap_content"   
  21.         android:text="测试Extras属性" 
  22.         />        
  23. </LinearLayout> 

second.xml

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical"   
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     >         
  7.     <TextView   
  8.         android:id="@+id/TextView1"   
  9.         android:layout_width="wrap_content"   
  10.         android:layout_height="wrap_content" 
  11.         /> 
  12. </LinearLayout> 

strings.xml

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <resources> 
  3.     <string name="hello">Hello World, FirstActivity!</string> 
  4.     <string name="app_name">IntentExtrasDemo</string> 
  5. </resources> 

FirstActivity.java

 
  1. package com.android.extras.activity;  
  2.  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10.  
  11. public class FirstActivity extends Activity {  
  12.     private Button btn;  
  13.     private EditText etx;  
  14.       
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.first);  
  19.           
  20.         btn = (Button)findViewById(R.id.Button1);  
  21.         etx = (EditText)findViewById(R.id.EditText1);  
  22.           
  23.         btn.setOnClickListener(new OnClickListener() {  
  24.             @Override  
  25.             public void onClick(View v) {  
  26.                 Intent intent = new Intent();  
  27.                 //设置Intent的class属性,跳转到SecondActivity  
  28.                 intent.setClass(FirstActivity.this, SecondActivity.class);  
  29.                 //为intent添加额外的信息  
  30.                 intent.putExtra("useName", etx.getText().toString());  
  31.                 //启动Activity  
  32.                 startActivity(intent);  
  33.             }  
  34.         });         
  35.     }  
  36. } 

SecondActivity.java

 
  1. package com.android.extras.activity;  
  2.  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.widget.TextView;  
  7.  
  8. public class SecondActivity extends Activity {  
  9.     private TextView tv;  
  10.       
  11.     @Override 
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         //设置当前的Activity的界面布局  
  15.         setContentView(R.layout.second);  
  16.         //获得Intent  
  17.         Intent intent = this.getIntent();         
  18.         tv = (TextView)findViewById(R.id.TextView1);  
  19.         //从Intent获得额外信息,设置为TextView的文本  
  20.         tv.setText(intent.getStringExtra("useName"));  
  21.     }  
  22. } 

注意:在添加第二个Activity SecondActivity的时候,要在AndroidManifest.xml里面添加上SecondActivity,具体如下,即是在15行</activity>的后面添加上16~18行的代码。如果不这样做,就会在模拟器上出现错误。

 
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  3.       package="com.android.extras.activity" 
  4.       android:versionCode="1" 
  5.       android:versionName="1.0"> 
  6.     <uses-sdk android:minSdkVersion="10" /> 
  7.  
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name"> 
  9.         <activity android:name=".FirstActivity" 
  10.                   android:label="@string/app_name"> 
  11.             <intent-filter> 
  12.                 <action android:name="android.intent.action.MAIN" /> 
  13.                 <category android:name="android.intent.category.LAUNCHER" /> 
  14.             </intent-filter> 
  15.         </activity> 
  16.         <activity android:name=".SecondActivity" 
  17.                   android:label="@string/app_name"> 
  18.         </activity> 
  19.     </application> 
  20. </manifest> 

效果图:

发送图片到会话给QQ好友和Intent本地使用 /** * 给QQ好友发送图片 * @param imagePath 图片路径 例如:"mnt/sdcard/test.gif" */ public void sendImageToQQ(String imagePath){ //创建查询条件的intent,用于查询哪些软件的有activity支持发送 Intent intent=new Intent(Intent 阅读详情

相关推荐

Intent 使用详解

和你一起终身学习,这里是程序员Android经典好文推荐,通过阅读本文,您将收获以下知识点:一、Intent 简介二、Intent 主要用途三、Intent 分类四、隐式Intent 接收过滤标签五、PendingIntent 介绍六、Intent的七大属性七、使用ADB调试 Intent一、Intent 简介Intent是一个消息传递对象,主要用于组件之间的通讯,例如:启动Activity、启...

程序员Android 775

【Android】Intent的使用及案例

Android的Intent对象是联系各个Activity的关键对象。Intent,翻译成中文就是“意图”,我们可以这样来理解Intent:通过Intent对象,来告诉Android要做什么。

m0_56233309的博客 4808

gps matlab 书,《GPS基本原理及其Matlab仿真》杨俊.pdf

本书讲述了全球定位系统(GPS)的基本原理和概念,具体包括:GPS测量原理、GPS的结构和发展历史;GPS伪距定位和载波相位定位的原理;GPS中使用的坐标系和时间系统;C/A码的实现原理及载波调制过程;GPS信号捕获和跟踪原理及实现;便携式GPS系统的设计与实现及常用芯片资料等。此外,本书还讲了利用Matlab仿真GPS中涉及到的各种算法和原理,并且给出了所有仿真源程序和仿真框图。利用Matlab...

weixin_33603105的博客 1176

Android开发学习笔记:Intent的简介以及属性的详解

一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。Intent不仅可用于应用程序之间,也可用于应用程序内部的A...

weixin_34226706的博客 550

AWS 小白书 [1] 全球架构

小白应该知道的 AWS 知识

boshansolo的博客 707

LAMMPS+Phonopy计算声子色散曲线

机器学习训练完势函数之后,需要对加以验证。对于晶体而言,色散曲线是其衡量计算是否准确的最重要标准,当然,还有能量,比热,RDF等性质。

qq_68842798的博客 3462

android Intent的简介以及属性的详解

android Intent的简介以及属性的详解

依然怡然 428

基于Dijkstra算法的共享单车调度优化系统设计与实现

根据最新统计数据,全球共享单车市场规模已超过300亿美元,中国作为最大的共享单车市场,日订单量超过5000万次。Dijkstra算法由荷兰计算机科学家Edsger W. Dijkstra于1956年提出,用于解决带权有向图中的单源最短路径问题。本文详细介绍了基于Dijkstra算法的共享单车调度优化系统的设计与实现。本文将详细介绍基于Dijkstra算法的共享单车调度优化系统的设计与实现,采用Java 17、Spring Boot 3.x等最新技术栈。

2201_76125393的博客 622

【转】【Java/Android】Intent的简介以及属性的详解

一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。Intent不仅可用于应用程序之间,也可用于应用程序内部的Activity/Ser...

kakarrot2009的专栏 121

LDRA Testbed软件静态分析_常见问题及处理

通过前面的几篇文章,LDRA Testbed软件静态分析的内容基本讲解完了,本篇文章将针对使用Testbed进行软件静态分析的常见问题进行介绍。 到此为止,LDRA Testbed软件静态分析的内容已经全部讲完,如果有问题,欢迎私信提问(不经常登录,回复可能不及时,见谅)。下一篇文章将开始讲解如何使用Testbed进行单元测试。

文章主要涉及嵌入式软件开发、测试、项目管理等,欢迎交流,共同进步。 2593

HTML满屏跳动的爱心(完整代码)

HTML语言实现满屏跳动爱心的完整代码。

AIGC 12万+
上一篇: Activity生命周期
下一篇: zookeeper
时冲
博客等级 码龄15年 12粉丝 35原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值