使用intent切换活动
来源:互联网 发布:中文域名查询 编辑:程序博客网 时间:2024/05/29 02:46
Intent之间的切换可以使用显示Intent和隐式Intent。
1、使用显示Intent
使用Intent创建Intent
的实例。构造函数的参数有两个,见下例:
bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intend=new Intent(MainActivity.this,Main2Activity.class); startActivity(intend); } });
参数1为当前启动活动的上下文,第二个参数为要启动的目标活动。创建好后,直接使用startActivity便可以启动。
2、隐式Intent
隐式Intent不会直接指定要启动的活动,而是在创建Intent时,指定action和category。然后startActivity就可以了。相应的活动根据配置文件中本activity中的intent_filter来确定启动哪一个活动。
在第一个活动中常见Intent实例并启动。
Button bt2=(Button)findViewById(R.id.button2); bt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intend=new Intent("com.example.david.activity3");//构造函数直接action。 intend.addCategory("com.example.david.activity_test");//使用addCategory将category添加到Intent中。startActivity(intend); } });//每个Intent只能指定一个action,却可以指定多个category。
待启动活动的intent_filter如下:
<intent-filter> <action android:name="com.example.david.activity3" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="com.example.david.activity_test" /> />
android.intent.category.DEFAULT是默认的category,在调用startActivity会自动将这个category加入到Intent中。
隐式Intent还可以启动其他程序的活动,是的android可以在多个应用程序之间进行切换。例如可以方便调用android系统中的一些内置动作。
(1)指定Intent中的action为Intent.ACTION_VIEW(常量值为android.intent.action.VIEW),再使用Intent的setData方法添加相应的Uri,便可以使用浏览器启动相应的页面。
bt3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intend=new Intent("android.intent.action.VIEW"); intend.setData(Uri.parse("http://www.baidu.com")); startActivity(intend); } });
(2)指定Intent中的action为Intent.ACTION_DIAL,再使用Intent的setData方法添加相应的Uri,便可以使用进入拨号的界面,并将相应的号码传入。
bt4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intend=new Intent(Intent.ACTION_DIAL); intend.setData(Uri.parse("tel:1566666666")); startActivity(intend); } });
对应于Intent中的setData,可以在待相应的活动中设置设置相应的数据类型。
<intent-filter> <action android:name="android.intent.action.VIEW" /> <data android:scheme="http" /> </intent-filter>
这里便可以响应http协议类型的数据。
其他的data标签如下:
androi:scheme 数据协议 (例:http)
android:host 数据的主机名部分(例:www.baidu,com)
android:port 数据的端口部分
android:path 端口之后的部分
android:mimeType 数据类型
只有标签中指定的的内容和Intent中携带的Data完全一致,当前活动才可以响应该Intent。
一般不用设置过多的内容,例如在本例中仅仅设置了android:scheme属性。
- 使用intent切换活动
- Android学习笔记:使用Intent切换活动
- 使用Intent来启动活动
- 使用Intent在活动之间穿梭
- 使用Intent在活动间穿梭 --7
- 在活动中使用Intent(笔记)
- 使用Intent在活动间传递数据
- 使用Intent在活动间穿梭
- 使用Intent进行活动间的跳转
- 活动——使用显式Intent
- 使用Intent在活动之间穿梭(Intent初识)
- 安卓·使用intent切换Activity
- 使用Intent调用其他活动的方法以及运作流程
- Android 使用Intent在活动之间传递数据
- 第2章 使用Intent在活动之间穿梭
- 使用Intent实现活动间的跳转和数据传递
- 22.使用Intent向下一个活动传递数据
- 23.使用Intent返回数据给上一个活动
- 2017.10思考
- TLD自定义标签步骤
- 通过网页链接调用QQ临时会话实现
- 最新版redis的安装及配置
- C++ 知识汇总
- 使用intent切换活动
- 什么是框架?
- 【STL】list源码剖析
- c++ 文件操作 fs.read 读出的字符数量不对,0x1A 在搞怪
- Visual Assist X 10.8.2001 破解版 支持VC2013及以下版本
- 应用框架
- python爬虫由浅入深15---利用Redis+Flask来维护代理池和Cookie池
- TableLayout的使用
- ActionBar(一)之ActionBar基础