android的intent使用方法
来源:互联网 发布:淘宝客户关系管理标签 编辑:程序博客网 时间:2024/05/17 22:03
android的intent使用方法
1. 说明
Android中提供了Intent机制来协助应用间或者应用程序内部的交互与通讯。
Intent的两种基本用法:一种是显式的Intent,即在构造Intent对象时就指定接收者,这种方式与普通的函数调用类似;另一种是隐式的Intent,即Intent的发送者在构造Intent对象时,并不知道接收者是谁,只是指出接收者的一些特性(比如说启动音乐播放软件)
2. 使用方法
1) 启动服务
a) 关键函数
context.startService()或context.bindService()
b) 示例
Intent i = new Intent(this, MyTestService.class);
this.startService(i); // 启动service
2) 发送广播
a) 关键函数
context.sendBroadcast()
b) 发送方
String msg = “test”;
Intent i = new Intent(“com.test.bc”);
i.pubExtra(“msg”, msg);
this.sendBroadcase(i);
c) 接收方
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_MEDIA_EJECT);
registerReceiver(mReceiver.interFilter);
3) 启动应用程序
a) 关键函数
context.startActivity()
b) 示例
Intent intent =
new Intent(“com.android.browser“, “com.android.browser.BrowserActivity“);
startActivity(intent);
3. Intent的组成
Intent的参数可多可少,系统根据不同的参数组合过滤出一个或多个适合规则的界面
1) 调用方:以下几个规则可以同时指定,也可以指定一部分或几部分
Component:指定包名类名来调用(见上例),它是晚绑定,不会在编译时报错
Action:指定做什么的规则(比如ACTION_DIAL指定拨号类型应用),以供过滤
Data:提供的重要数据,通常是Uri,同时也提供数据的类型,以供过滤
Type:用于指定类型,以供过滤(比如ACTION_VIEW同时指定为Type为Image,则调出浏览图片的应用)
Category:指定范围
Extras:通过Bundle类传参, 数据多,数据量大时用它传
Flags:标志位(比如FLAG_ACTIVITY_NEW_TASK指定新开一个任务)
2) 被调用方
在AndroidManifest.xml中的<intent-filter>中声明规则
例如: 一般程序都需要在inter-filter中加入android.intent.category.LAUNCHER的声明, 以便被程序启动器(Launcher)识别, 即以点击图标的方式供用户运行
3) 示例
Intent intent = new Intent();
intent.setClassName(“com.android.browser“,
“com.android.browser.BrowserActivity“); // 打开浏览器
Uri data = Uri.parse(“http://www.google.com“);
intent.setData(data); // 打开某网页
intent.addFlag(Intent.FLAG_ACTIVITY_NEW_TASK); // 以新建任务方式打开
intent.setAction(Intent.ACTION_VIEW); // 以浏览方式打开
startActivity(intent);
4. Intent的源码实现
1) Intent解析,过滤规则对应出具体应用
frameworks/base/core/java/android/content/IntentFilter.java
2) Intent定义,规定程序中的使用的Define与xml中字串的对应关系
frameworks/base/core/java/android/content/Intent.java
5. 参考
http://zhubin215130.javaeye.com/blog/614913
- android的intent使用方法
- android的intent使用方法
- android的intent使用方法
- android的intent使用方法 .
- android的intent使用方法
- Intent在Android的一些使用方法
- Android中Intent的几种使用方法
- Android Intent意图的常用使用方法
- 详解Android中Intent的使用方法
- android中intent的基本使用方法
- android intent使用方法
- Android Intent 使用方法
- Android: Intent详细使用方法
- intent的使用方法
- Android开发之Intent使用方法
- Android 系统应用调用,intent 的使用方法总结
- 详解Android应用开发中Intent的作用及使用方法
- android学习:通过Intent打开Activity的两种方法,Intent过滤器的使用方法
- 一张图片汇聚全部linux常用命令
- java中defalut和friendly的区别
- Reuse your Hibernate/JPA domain model with Grails
- Struts框架中的表单验证与资源文件
- String.format()的用法
- android的intent使用方法
- 为CEikEdwin绘制标准边框
- MFC 对Excel的基本操作
- 通过代码来申请CA证书
- 三个timer相关的类
- VC快捷键
- eval用法
- 50个Demo展示HTML5无穷的魅力
- IT人职业道德的反思