Android用Intent这个特殊类实现Activity与Activity之间的切换。Intent类用于描述应用的功能。在Intent的描述结构中,有两个最重要的部分:动作和动作对应的数据。

  Intent作用的表现形式为:
  1、通过Context.startActivity() orActivity.startActivityForResult() 启动一个Activity;
  2、通过 Context.startService() 启动一个服务,或者通过Context.bindService() 和后台服务交互;
  3、通过广播方法(比如 Context.sendBroadcast(),Context.sendOrderedBroadcast(), Context.sendStickyBroadcast()) 发给broadcast receivers。

  Intent属性的设置,包括以下几点:(以下为XML中定义,当然也可以通过Intent类的方法来获取和设置)

一、Action,也就是要执行的动作

  SDk中定义了一些标准的动作,包括:

当然,也可以自定义动作。

二、Data,也就是执行动作要操作的数据

  Android中采用指向数据的一个URI来表示,如在联系人应用中,一个指向某联系人的URI可能为:content://contacts/1。对于不同的动作,其URI数据的类型是不同的(可以设置type属性指定特定类型数据),如 ACTION_EDIT指定Data为文件URI,打电话为tel:URI,访问网络为http:URI,而由content provider提供的数据则为content:URIs。

三、type(数据类型),显式指定Intent的数据类型(MIME)。

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

四、category(类别),被执行动作的附加信息。

  例如 LAUNCHER_CATEGORY 表示Intent 的接受者应该在Launcher中作为顶级应用出现;而ALTERNATIVE_CATEGORY表示当前的Intent是一系列的可选动作中的一个,这些动作可以在同一块数据上执行。还有其他的为:



五、component(组件),指定Intent的的目标组件的类名称。

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

六、extras(附加信息),是其它所有附加信息的集合。

  Intent的解析机制可参考上一篇文章:http://www.cnblogs.com/lurenjiashuo/archive/2011/03/09/1978260.html

七、Android Intent常用用法汇总

  1、显示网页:

Uri uri = Uri.parse("http://www.cnblogs.com"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it);

2、显示地图:

Uri uri = Uri.parse("geo:38.899533,-77.036476"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it);

3、路径规划:

Uri uri = Uri.parse("http://maps.google.com/maps? f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it)

4、打电话: //叫出拨号程序 Uri uri = Uri.parse("tel:0800000123"); Intent it = new Intent(Intent.ACTION_DIAL, uri); startActivity(it); //直接打电话出去 Uri uri = Uri.parse("tel:0800000123"); Intent it = new Intent(Intent.ACTION_CALL, uri); startActivity(it); //用這個,要在 AndroidManifest.xml 中,加上 //<uses-permission id="android.permission.CALL_PHONE" />   5、传送SMS/MMS: //调用短信程序 Intent it = new Intent(Intent.ACTION_VIEW, uri); it.putExtra("sms_body", "The SMS text"); it.setType("vnd.android-dir/mms-sms"); startActivity(it); //传送消息 Uri uri = Uri.parse("smsto://0800000123"); Intent it = new Intent(Intent.ACTION_SENDTO, uri); it.putExtra("sms_body", "The SMS text"); startActivity(it); //传送 MMS Uri uri = Uri.parse("content://media/external/images/media/23"); Intent it = new Intent(Intent.ACTION_SEND); it.putExtra("sms_body", "some text"); it.putExtra(Intent.EXTRA_STREAM, uri); it.setType("image/png"); startActivity(it);   6、传送 Email: //方法一 Uri uri = Uri.parse("mailto:xxx@abc.com"); Intent it = new Intent(Intent.ACTION_SENDTO, uri); startActivity(it); //方法二 Intent it = new Intent(Intent.ACTION_SEND); it.putExtra(Intent.EXTRA_EMAIL, "me@abc.com"); it.putExtra(Intent.EXTRA_TEXT, "The email body text"); it.setType("text/plain"); startActivity(Intent.createChooser(it, "Choose Email Client")); //方法三 Intent it=new Intent(Intent.ACTION_SEND); String[] tos={"me@abc.com"}; String[] ccs={"you@abc.com"}; it.putExtra(Intent.EXTRA_EMAIL, tos); it.putExtra(Intent.EXTRA_CC, ccs); it.putExtra(Intent.EXTRA_TEXT, "The email body text"); it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text"); it.setType("message/rfc822"); startActivity(Intent.createChooser(it, "Choose Email Client")); //传送附件 Intent it = new Intent(Intent.ACTION_SEND); it.putExtra(Intent.EXTRA_SUBJECT, "The email subject text"); it.putExtra(Intent.EXTRA_STREAM, "file:///sdcard/mysong.mp3"); sendIntent.setType("audio/mp3"); startActivity(Intent.createChooser(it, "Choose Email Client"));   7、播放多媒体: //方法一 Uri uri = Uri.parse("file:///sdcard/song.mp3"); Intent it = new Intent(Intent.ACTION_VIEW, uri); it.setType("audio/mp3"); startActivity(it); //方法二 Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it);   8、Market 相关: //寻找某个应用 Uri uri = Uri.parse("market://search?q=pname:pkg_name"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it); //where pkg_name is the full package path for an application //显示某个应用的相关信息 Uri uri = Uri.parse("market://details?id=app_id"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it); //where app_id is the application ID, find the ID //by clicking on your application on Market home //page, and notice the ID from the address bar   9、Uninstall 应用程序: Uri uri = Uri.fromParts("package", strPackageName, null); Intent it = new Intent(Intent.ACTION_DELETE, uri); startActivity(it);


更多相关文章

  1. ListView 实现阻尼回弹效果 并去除边缘阴影
  2. Android(安卓)Studio TV开发教程(十)添加引导步骤
  3. Android之开源中国客户端源码分析(一)
  4. clearTaskOnLaunch的使用
  5. Android初学笔记(记录自己的学习过程,有不对的地方欢迎指出)
  6. Android(安卓)Manifest.xml 结构详解
  7. # Android中Activity四种启动模式和taskAffinity属性详解 #(2)
  8. Android(安卓)ActionBar完全解析,使用官方推荐的最佳导航栏(下)
  9. Android(安卓)安卓动画 属性动画 - 移动动画

随机推荐

  1. Android Audio代码分析19 - setPosition
  2. Android设置Button保持Press的状态
  3. android -- 小功能 解决自动旋转导致acti
  4. android调用系统的分享接口
  5. Android(安卓)API Guides---OpenGL ES
  6. Android Service Bind启动调用service方
  7. taintDroid系统性能测试之——Android AV
  8. Android中TabHost部件使用
  9. Android 退出程序方式——再按一次退出
  10. Android简单例子(拨号服务调用)