android中意图(intent)就是告诉系统要做某件事情。比如要拨打电话或者发送短信。
或者在一个Activity中点击按钮跳转到另外一个activity时也用到意图。
意图分为两种:显示意图和隐式意图
显示意图:
       在构造意图时直接指定意图的class,则这种方式为显示意图。如:
            Intent intent=new Intent(context,OtherActivity.class);
这种情况是在同一个应用中从一个意图调用另外一个意图时可以这么使用。
隐式意图:
       此意图构造时不指定意图的class,而是制定一个名字和类别或者还有数据格式等,这些名字类别数据格式是在应用的功能清单中申明的。
        <activity android:name=".OtherActivity" android:label="你好">
            <intent-filter>
                <action android:name="org.main.actions.Other"/>
                <category android:name="org.main.categories.Other"/>
                <category android:name="android.intent.category.DEFAULT"/>   <!--不可少 -->
                <data android:scheme="num" android:host="www.xxx.cn" android:path="/files"/>
            </intent-filter>
        </activity>
如上面这个activity。我们在别的activity里要调用这个activity时,使用隐式的方法就这么调用:
   Intent intent=new Intent();
   intent.setAction("org.main.actions.Other");
   intent.addCategory("org.main.categories.Other");
//另外由于我们声明了data在intent-filter中,所以还需要为intent 设置数据(data),且数据需要与data标签中指定的数据结构类型和主机名还有路径一致
   intent.setData(Uri.parse("num://www.xxx.cn/files/992034.gif"));
   startActivity(intent);       //这个方法里面会默认调用 intent.addCategory("android.intent.category.DEFAULT");,所以在编写intent的filter的使用android.intent.category.DEFAULT这个category必须声明。
 

1.Intent作用

Intent是一个将要执行的动作的抽象的描述,由Intent来协助完成android各个组件之间的通讯。比如调用Activity实例化对象的startActivity()来启动一个activity,或者由broadcaseIntent()来传递给所有感兴趣的BroadcaseReceiver, 或者由startService()/bindservice()来启动一个后台的service。可见,intent主要用来启动activity或者service(并携带需要传递的参数信息),intent理解成activity之间的粘合剂。
总之,Intent具有激活组件和携带数据的功能

2.Intent形式

(1).显示意图(Explicit Intents)

明确指定组件名的Intent为显式意图,指定了Intent应该传递给那个组件。通过下面代码方式,可以创建显示意图实例化对象,并设定需要传递的参数信息。由于显示意图指定了具体的组件对象,不需要设置intent的其它意图过滤对象。

  1. //  1.创建Intent实例化对象几种方式
  2. Intent intent = new Intent();
  3. intent.setClass(Context packageContext, Class<?> cls) ;           //内部调用setComponent(ComponentName)
  4. intent.setClassName(Context packageContext, String className) ; //内部调用setComponent(ComponentName)
  5. intent.setClassName(String packageName, String className) ;     //内部调用setComponent(ComponentName),可以激活外部应用
  6. intent.setComponent(new ComponentName(this, Class<?> cls));
  7. intent.setComponent(new ComponentName(this, "package Name"));

(2).隐式意图(Implicit Intents)

没有明确指定组件名的Intent为隐式意图,系统会根据隐式意图中设置的 动作(action)、类别(category)、数据URI等来匹配最合适的组件。

1).action

The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, 包括Android 系统指定的 和 自定义

  1. intent.setAction("com.baidu.action.TEST");
  1. <action android:name="com.baidu.action.TEST"/>

2).data

expressed as a Uri, The data to operate on, such as a person record in the contacts database.

系统自带的Action简单举例

Action Data(Uri) Content

ACTION_VIEW

content://contacts/people/1

Display information about the person whose identifier is "1".

ACTION_VIEW

tel:123

Display the phone dialer with the given number filled in.

ACTION_DIAL

tel:123

Display the phone dialer with the given number filled in.

自定义data匹配

  1. intent.setData(Uri.parse("baidu://www.baidu.com/news"));
  1. <!-- android:path 内容字符串需要以 / 开头 -->
  2. <data android:scheme="baidu" android:host="www.baidu.com" android:path="/news"/>

3).category

Gives additional information about the action to execute.
注意:项目清单的xml文件意图过滤器中必须指定 android.intent.category.DEFAULT类别,Activities will very often need to support the CATEGORY_DEFAULT so that they can be found by Context.startActivity,or Context can't the acitivity component

  1. intent.addCategory("com.baidu.category.TEST");
  1. <!-- 必须指定CATEGORY_DEFAULT,只有这样startActivity(intent)才能找到 -->
  2. <category android:name="com.baidu.category.TEST" />
  3. <category android:name="android.intent.category.DEFAULT" />

除了以上主要属性外,下面还有其它属性可以额外增强。

4).type

Specifies an explicit type (a MIME type) of the intent data.

  1. intent.setType("image/jpeg");
  1. <data android:mimeType="image/*" />

注意:java文件中data Uri 和 type不能同时使用各自的函数进行设定,因为使用type时会把Uri清除掉,可以使用setDataAndType方法设定

  1. intent.setDataAndType(Uri.parse("baidu://www.baidu.com/news"), "image/jpeg");
  1. <data android:mimeType="image/*" android:scheme="baidu" android:host="www.baidu.com" android:path="/news"/>

(3).两者的使用区别

显式意图一般在应用的内部使用,因为在应用内部已经知道了组件的名称,直接调用就可以了。

当一个应用要激活另一个应用中的Activity时,只能使用隐式意图,根据Activity配置的意图过滤器建一个意图,让意图中的各项参数的值都跟过滤器匹配,这样就可以激活其他应用中的Activity。所以,隐式意图是在应用与应用之间使用的。

3.Activity的Intent数据传递

  1. //Activity间的数据传递
  2. //  1.直接向intent对象中传入键值对(相当于Intent对象具有Map键值对功能)
  3. intent.putExtra("first", text1.getText().toString());
  4. intent.putExtra("second", text2.getText().toString());
  5. //  2.新建一个Bundle对象 ,想该对象中加入键值对,然后将该对象加入intent中
  6. Bundle bundle = new Bundle();
  7. bundle.putString("first", "zhang");
  8. bundle.putInt("age", 20);
  9. intent.putExtras(bundle);
  10. //  3.向intent中添加ArrayList集合对象
  11. intent.putIntegerArrayListExtra(name, value);
  12. intent.putIntegerArrayListExtra(name, value);
  13. //  4.intent传递Object对象(被传递的对象的类实现Parcelable接口,或者实现Serialiable接口)
  14. public Intent putExtra(String name, Serializable value)
  15. public Intent putExtra(String name, Parcelable value)

4.Activity退出的返回结果

  1. //  1.通过startActivityForResult方式启动一个Activity
  2. MainActivity.this.startActivityForResult(intent, 200);  //intent对象,和  requestCode请求码
  3. //  2.新activity设定setResult方法,通过该方法可以传递responseCode 和 Intent对象
  4. setResult(101, intent2);                                //responseCode响应码 和 intent对象
  5. //  3.在MainActivity中覆写onActivityResult方法,新activity一旦退出,就会执行该方法
  6. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  7. Toast.makeText(this, data.getStringExtra("info")+"requestCode:"+requestCode+"resultCode:"+resultCode, Toast.LENGTH_LONG).show();
  8. }

5.Intent常见应用(转)

(1).调用拨号程序

  1. Uri uri = Uri.parse("tel:10086");
  2. Intent intent = new Intent(Intent.ACTION_DIAL, uri);
  3. startActivity(intent);

(2).发送短信或者彩信

  1. //发生短信
  2. Uri uri = Uri.parse("smsto:10086");
  3. Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
  4. intent.putExtra("sms_body", "Hello");
  5. startActivity(intent);
  6. //发送彩信,相当于发送带附件的短信
  7. Intent intent = new Intent(Intent.ACTION_SEND);
  8. intent.putExtra("sms_body", "Hello");
  9. Uri uri = Uri.parse("content://media/external/images/media/23");
  10. intent.putExtra(Intent.EXTRA_STREAM, uri);
  11. intent.setType("image/png");
  12. startActivity(intent);

(3).通过浏览器打开网页

  1. Uri uri = Uri.parse("http://www.google.com");
  2. Intent intent  = new Intent(Intent.ACTION_VIEW, uri);
  3. startActivity(intent);

(4).发送电子邮件

  1. Uri uri = Uri.parse("mailto:someone@domain.com");
  2. Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
  3. startActivity(intent);
  4. //给someone@domain.com发邮件发送内容为“Hello”的邮件
  5. Intent intent = new Intent(Intent.ACTION_SEND);
  6. intent.putExtra(Intent.EXTRA_EMAIL, "someone@domain.com");
  7. intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
  8. intent.putExtra(Intent.EXTRA_TEXT, "Hello");
  9. intent.setType("text/plain");
  10. startActivity(intent);
  11. // 给多人发邮件
  12. Intent intent=new Intent(Intent.ACTION_SEND);
  13. String[] tos = {"1@abc.com", "2@abc.com"}; // 收件人
  14. String[] ccs = {"3@abc.com", "4@abc.com"}; // 抄送
  15. String[] bccs = {"5@abc.com", "6@abc.com"}; // 密送
  16. intent.putExtra(Intent.EXTRA_EMAIL, tos);
  17. intent.putExtra(Intent.EXTRA_CC, ccs);
  18. intent.putExtra(Intent.EXTRA_BCC, bccs);
  19. intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
  20. intent.putExtra(Intent.EXTRA_TEXT, "Hello");
  21. intent.setType("message/rfc822");
  22. startActivity(intent);

(5).显示地图与路径规划

  1. // 打开Google地图中国北京位置(北纬39.9,东经116.3)
  2. Uri uri = Uri.parse("geo:39.9,116.3");
  3. Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  4. startActivity(intent);
  5. // 路径规划:从北京某地(北纬39.9,东经116.3)到上海某地(北纬31.2,东经121.4)
  6. Uri uri = Uri.parse("http://maps.google.com/maps?f=d&saddr=39.9 116.3&daddr=31.2 121.4");
  7. Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  8. startActivity(intent);

(6).播放多媒体

  1. Intent intent = new Intent(Intent.ACTION_VIEW);
  2. Uri uri = Uri.parse("file:///sdcard/foo.mp3");
  3. intent.setDataAndType(uri, "audio/mp3");
  4. startActivity(intent);
  5. Uri uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
  6. Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  7. startActivity(intent);

(7).拍照

  1. // 打开拍照程序
  2. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  3. startActivityForResult(intent, 0);
  4. // 取出照片数据
  5. Bundle extras = intent.getExtras();
  6. Bitmap bitmap = (Bitmap) extras.get("data");

(8).获取并剪切图片

  1. // 获取并剪切图片
  2. Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
  3. intent.setType("image/*");
  4. intent.putExtra("crop", "true"); // 开启剪切
  5. intent.putExtra("aspectX", 1); // 剪切的宽高比为1:2
  6. intent.putExtra("aspectY", 2);
  7. intent.putExtra("outputX", 20); // 保存图片的宽和高
  8. intent.putExtra("outputY", 40);
  9. intent.putExtra("output", Uri.fromFile(new File("/mnt/sdcard/temp"))); // 保存路径
  10. intent.putExtra("outputFormat", "JPEG");// 返回格式
  11. startActivityForResult(intent, 0);
  12. // 剪切特定图片
  13. Intent intent = new Intent("com.android.camera.action.CROP");
  14. intent.setClassName("com.android.camera", "com.android.camera.CropImage");
  15. intent.setData(Uri.fromFile(new File("/mnt/sdcard/temp")));
  16. intent.putExtra("outputX", 1); // 剪切的宽高比为1:2
  17. intent.putExtra("outputY", 2);
  18. intent.putExtra("aspectX", 20); // 保存图片的宽和高
  19. intent.putExtra("aspectY", 40);
  20. intent.putExtra("scale", true);
  21. intent.putExtra("noFaceDetection", true);
  22. intent.putExtra("output", Uri.parse("file:///mnt/sdcard/temp"));
  23. startActivityForResult(intent, 0);

(9).打开Google Market

  1. // 打开Google Market直接进入该程序的详细页面
  2. Uri uri = Uri.parse("market://details?id=" + "com.demo.app");
  3. Intent intent = new Intent(Intent.ACTION_VIEW, uri);
  4. startActivity(intent);

(10).安装和卸载程序

  1. Uri uri = Uri.fromParts("package", "com.demo.app", null);
  2. Intent intent = new Intent(Intent.ACTION_DELETE, uri);
  3. startActivity(intent);

(11).进入设置界面

    1. // 进入无线网络设置界面(其它可以举一反三)
    2. Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
    3. startActivityForResult(intent, 0);

Android开发之意图解析的更多相关文章

  1. Android开发MVP模式解析

    http://www.cnblogs.com/bravestarrhu/archive/2012/05/02/2479461.html 在开发Android应用时,相信很多同学遇到和我一样的情况,虽然 ...

  2. Android开发中如何解析Json

    解析Json 有了请求,自然会有响应,那我们该如何解析服务端响应返回给我们的Json数据呢? 了解什么是Json JSON(JavaScript object notation)是一种轻量级的数据交换 ...

  3. Android开发新手HelloWorld解析

    首先看这个 HelloWorld 类. Java代码public class HelloWorld extends Activity {       /** Called when the activ ...

  4. android开发之意图

    intent 全局变量传值(程序关闭时存储值消失) intent普通传值 intent传值 intent不能序列化传值 intent回传

  5. Android开发面试经——6.常见面试官提问Android题②(更新中...)

    版权声明:本文为寻梦-finddreams原创文章,请关注:http://blog.csdn.net/finddreams 关注finddreams博客:http://blog.csdn.net/fi ...

  6. Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml

    Android开发:碎片Fragment完全解析   为了让界面可以在平板上更好地展示,Android在3.0版本引入了Fragment(碎片)功能,它非常类似于Activity,可以像 Activi ...

  7. [android开发IDE]adt-bundle-windows-x86的一个bug:无法解析.rs文件--------rs_core.rsh file not found

    google的android自带的apps写的是相当牛逼的,将其导入到eclipse中方便我们学习扩展.可惜关于导入的资料太少了,尤其是4.1之后的gallery和camera合二为一了.之前导4.0 ...

  8. Android开发周报:Flyme OS开源、经典开源项目解析

    Android开发周报:Flyme OS开源.经典开源项目解析 新闻 <魅族Flyme OS源码上线Github> :近日魅族正式发布了MX5,并且在发布会上,魅族还宣布Flyme OS开 ...

  9. 【Android开发日记】之入门篇(十三)——Android的控件解析

    Android的控件都派生自android.view.View类,在android.widget包中定义了大量的系统控件供开发者使用,开发者也可以从View类及其子类中,派生出自定义的控件. 一.An ...

随机推荐

  1. 【技术贴】关闭CMD错误提示声音

    关掉后,整个世界清静多了. cmd打开后 1. 禁用“嘀嘀”声的设备来源,这是由beep驱动服务所提供,可以将beep驱动的启动类型设置为禁用,可以打开CMD窗口,运行以下命令:永久禁用错误声音 sc ...

  2. CISCO2691的OSPF点对点密文测评测试

    都差不多,粘一个文件就能说明问题了. Router#show run Building configuration... Current configuration : bytes ! version ...

  3. 【HDU 1542】Atlantis 矩形面积并(线段树,扫描法)

    [题目] Atlantis Problem Description There are several ancient Greek texts that contain descriptions of ...

  4. JavaScript 作用域和变量提升

    本文是这篇文章的简单翻译. 如果按照下面的代码按照JavaScript程序的执行方式执行,alert函数会显示什么? var foo = 1; function bar() { if (!foo) { ...

  5. Apache ActiveMQ消息中间件的基本使用

    Apache ActiveMQ是Apache软件基金会所研发的开放源码消息中间件:由于ActiveMQ是一个纯Java程式,因此只需要操作系统支援Java虚拟机,ActiveMQ便可执行. 支持Jav ...

  6. 转:三十一、Java图形化界面设计——布局管理器之GridLayout(网格布局)

    http://blog.csdn.net/liujun13579/article/details/7772491 网格布局特点: l  使容器中的各组件呈M行×N列的网格状分布. l  网格每列宽度相 ...

  7. Microsoft SQL Server 2008 安装图解(Windows 7)

    简介 本文详细记录了一次完整的Microsoft SQL Server 2008在Windows 7操作系统上的安装过程.注意:Microsoft SQL Server 2008与Windows 7操 ...

  8. EJB 总结学习(1)

    总结1: 以下面这行代码为例: PersonDaoBeanRemote pdb = (PersonDaoBeanRemote)ctx.lookup("PersonDaoBean/remote ...

  9. 【转】shell 教程——01 Shell简介:什么是Shell,Shell命令的两种执行方式

    Shell本身是一个用C语言编写的程序,它是用户使用Unix/Linux的桥梁,用户的大部分工作都是通过Shell完成的.Shell既是一种命令语言,又是一种程序设计语言.作为命令语言,它交互式地解释 ...

  10. 【转】漫谈ANN(2):BP神经网络

    上一次我们讲了M-P模型,它实际上就是对单个神经元的一种建模,还不足以模拟人脑神经系统的功能.由这些人工神经元构建出来的网络,才能够具有学习.联想.记忆和模式识别的能力.BP网络就是一种简单的人工神经 ...