【Android学习】Intent初使用
来源:互联网 发布:辐射4优化差 编辑:程序博客网 时间:2024/05/17 08:17
Intent简介
Intent是Android程序中各组件之间进行交互的一种重要方式,它不仅可以指明当前组件想要执行的动作,还可以在不同组件之间传递数据。Intent一般可以被用于启动活动、启动服务以及发送广播等场景,本次就介绍一下Intent启动活动的过程。
Intent分类
Intent大致可以分为两种:显式Intent和隐式Intent
显式Intent
Intent有多个函数构造的重载,其中一个是Intent(Context packageContext,Class<?>
cls)。第一个参数是提供启动活动的上下文,第二个参数则是直接指定想要启动的目标活动。这里要用到Activity类中的startActivity()方法,这个方法专门用于启动活动的
我们想在第一个活动中显式的启动第二个活动,在FirstActivity中添加按钮的监听事件,代码如下
public class FIrstActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first_layout); Button button=(Button) findViewById(R.id.button_1); button.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ Intent intent=new Intent(FIrstActivity.this,SecondACtivity.class); startActivity(intent); } }); }}
即可在FIrstActicity这个活动的基础上打开SecondActivity
按下START2跳转到SecondActivity
SecondActivity
使用这种方式来启动活动,Intent的“意图”非常明显,故而称为显式Intent
隐式Intent
相比于显式Intent,隐式Intent就含蓄了很多,它并不明确指出我们想要启动哪一个活动,而是指定了一系列更为抽象的action和category等信息,然后交由系统去分析这个Intent,并帮我们找到合适的活动去启动。
通过在activity 标签下配制intent-filter的内容,可以指定当前活动能够响应的action和category,编辑AndroidManifest.xml,添加代码如下
<activity android:name=".SecondActivity" android:label="SecondStep"> <intent-filter> <action android:name="com.example.activitytest.ACTION_START" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
在action标签中我们指明了当前活动可以响应com.example.activitytest.ACTION_START这个action,而category标签则包含了当前活动能响应的Intent中带有的category,只有两者同时匹配,这个活动才能响应Intent,这里的android.intent.category.DEFAULT是一种默认的category,在调用startActivity()方法的时候会自动将这个category添加到Intent中
与此同时,修改FIrstActivity中按钮的点击事件:
Button button2=(Button) findViewById(R.id.button_start2); button2.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ Intent intent=new Intent("com.example.activitytest.ACTION_START"); startActivityForResult(intent,1); } }
同样也会得到之前显式Intent一样的结果
注:每个Intent中只能指定一个action,但却能指定多个category。
更多的隐式Intent的用法
我们不仅可以启动自己程序内的活动,还可以启动其他程序的活动,比如说我想在应用程序中打开一个网页
修改FIrstActivity中按钮点击事件的代码:
Button button=(Button) findViewById(R.id.button_1); button.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ Toast.makeText(FIrstActivity.this,"u click it!",Toast.LENGTH_SHORT).show(); Intent intent=new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://www.qq.com")); startActivity(intent); } });
这里我们首先指定了action是Intent.ACTION_VIEW,这是一个Android系统内置动作,其常量值为android.intent.action.VIEW,然后通过uri.parse()方法,将一个网址字符串解析成一个Uri对象,再调用Intent的setData()方法将这个Uri对象传递进去。
重新进行程序,在FIrstActivity界面点击按钮就可以看打开了qq主页。
我们还可以在intent-filter中添加data标签,让当前活动能够响应什么类型的数据,data标签主要可以配置以下内容。
android:scheme 用于指定数据的协议部分
android:host 用于指定数据的主机名部分
android:port 用于指定数据的端口部分
android:path 用于指定主机名和端口之后的部分
android:mimeType 用于指定可以处理的数据类型
不妨实践一下,新建一个ThirdActivity 然后修改AndroidManifest.xml中的注册信息如下:
<activity android:name=".ThirdActivity" android:label="ThirdStep"> <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="http"/> </intent-filter> </activity>
这时我们再重新启动程序点击按钮就能看到
ThirdStep已经能够响应打开网页的Intent了
除了Http协议外,还可以指定很多其他协议,比如geo表示地理位置,tel表示拨打电话等等,相信这在以后的应用开发中会经常用到吧。
参考书目:《第一行代码》-郭霖
- 【Android学习】Intent初使用
- Android学习 - Intent使用
- Android学习之Intent使用
- Android学习之Intent使用
- Android学习之Intent使用
- [Android--Code]Intent的学习与使用
- Android学习之Intent过滤器的使用
- Android学习笔记----使用Intent传递对象
- 学习Android中Intent的使用
- android学习之路-使用Intent跳转
- android学习4#--使用Intent创建Activity
- Android学习笔记:使用Intent切换活动
- Android学习笔记--3.Intent的使用
- Android学习笔记(4)-学习Intent的使用
- Android学习笔记(4)-学习Intent的使用
- Android学习笔记-学习Intent的使用
- Android学习笔记--Intent
- Android Intent 机制学习
- Mybatis架构
- Java:如何在命令行输入数据
- scala的模式匹配
- linux异步IO
- 微信公众号开发(1)--微信公众平台与后台服务器
- 【Android学习】Intent初使用
- 数组阻塞队列 ArrayBlockingQueue
- GYP 简介
- 1638 function overloading
- Magento 2中文文档教程
- java中输入的日期打印出星期几,Date互相转换String
- Angular 服务器端渲染中的坑
- spring+springMVC框架搭建(一)
- poj 1273 Drainage Ditches 网络流 Edmond Karp || Dinic