androidintent使用方法

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同时指定为TypeImage,则调出浏览图片的应用)
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定义,规定程序中的使用的Definexml中字串的对应关系
frameworks/base/core/java/android/content/Intent.java

5. 参考
http://zhubin215130.javaeye.com/blog/614913

更多相关文章

  1. Android开发之旅: Intents和Intent Filters(理论部分)
  2. android进程间服务通信示例
  3. adb logcat命令查看并过滤android输出log
  4. Android(安卓)NFC开发(二)——Android世界里的NFC所具备的条件以及
  5. Android之隐式意图(Intent)如何查找匹配的组件
  6. android进程间服务通信
  7. Android(安卓)解析CSV文件,中文乱码
  8. android调用浏览器打开网页链接
  9. MediaExtractor的seekTo方法精确定位到指定帧

随机推荐

  1. android input 事件传递主要流程
  2. Android中获取屏幕的宽和高
  3. Android(安卓)studio 中调用ndk-build 进
  4. Windows下Android开发环境配置的一种方法
  5. Android通知栏消息(基本文字通知)
  6. Android中通过浏览器打开一个网页
  7. MTK Android(安卓)Driver :camera
  8. API指南----application
  9. Android字体闪烁动画(线程)
  10. Android获取本机信息(随时更新)