zoukankan      html  css  js  c++  java
  • Android_Intent意图详解

    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.创建Intent实例化对象几种方式
    
    Intent intent = new Intent();
    intent.setClass(Context packageContext, Class<?> cls) ;			//内部调用setComponent(ComponentName)
    intent.setClassName(Context packageContext, String className) ;	//内部调用setComponent(ComponentName)
    intent.setClassName(String packageName, String className) ;		//内部调用setComponent(ComponentName),可以激活外部应用
    
    intent.setComponent(new ComponentName(this, Class<?> cls));
    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 系统指定的 和 自定义

    intent.setAction("com.baidu.action.TEST");
    <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简单举例

    ActionData(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匹配

    intent.setData(Uri.parse("baidu://www.baidu.com/news"));
    <!-- android:path 内容字符串需要以 / 开头 -->
    <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

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

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

    4).type

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

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

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

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

    (3).两者的使用区别

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

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

    3.Activity的Intent数据传递

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

    4.Activity退出的返回结果

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

    5.Intent常见应用(转)

    (1).调用拨号程序

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

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

    //发生短信
    Uri uri = Uri.parse("smsto:10086"); 
    Intent intent = new Intent(Intent.ACTION_SENDTO, uri); 
    intent.putExtra("sms_body", "Hello"); 
    startActivity(intent); 
    
    //发送彩信,相当于发送带附件的短信
    Intent intent = new Intent(Intent.ACTION_SEND); 
    intent.putExtra("sms_body", "Hello"); 
    Uri uri = Uri.parse("content://media/external/images/media/23"); 
    intent.putExtra(Intent.EXTRA_STREAM, uri); 
    intent.setType("image/png"); 
    startActivity(intent); 

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

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

    (4).发送电子邮件

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

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

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

    (6).播放多媒体

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

    (7).拍照

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

    (8).获取并剪切图片

    // 获取并剪切图片 
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("image/*"); 
    intent.putExtra("crop", "true"); // 开启剪切 
    intent.putExtra("aspectX", 1); // 剪切的宽高比为1:2 
    intent.putExtra("aspectY", 2); 
    intent.putExtra("outputX", 20); // 保存图片的宽和高 
    intent.putExtra("outputY", 40);  
    intent.putExtra("output", Uri.fromFile(new File("/mnt/sdcard/temp"))); // 保存路径 
    intent.putExtra("outputFormat", "JPEG");// 返回格式 
    startActivityForResult(intent, 0); 
    
    // 剪切特定图片 
    Intent intent = new Intent("com.android.camera.action.CROP");  
    intent.setClassName("com.android.camera", "com.android.camera.CropImage");  
    intent.setData(Uri.fromFile(new File("/mnt/sdcard/temp")));  
    intent.putExtra("outputX", 1); // 剪切的宽高比为1:2 
    intent.putExtra("outputY", 2); 
    intent.putExtra("aspectX", 20); // 保存图片的宽和高 
    intent.putExtra("aspectY", 40); 
    intent.putExtra("scale", true); 
    intent.putExtra("noFaceDetection", true);  
    intent.putExtra("output", Uri.parse("file:///mnt/sdcard/temp"));  
    startActivityForResult(intent, 0); 

    (9).打开Google Market

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

    (10).安装和卸载程序

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

    (11).进入设置界面

    // 进入无线网络设置界面(其它可以举一反三)   
    Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);   
    startActivityForResult(intent, 0); 
  • 相关阅读:
    新型肺炎实时动态
    大学排名数据爬取
    python BeautifulSoup基本用法
    爬虫爬取
    人口普查系统--信息查找
    人口普查系统--信息删除
    人口普查系统--信息修改
    人口普查系统--信息登记
    期中考试题目
    期中考试前准备--数据库查找代码
  • 原文地址:https://www.cnblogs.com/riskyer/p/3331203.html
Copyright © 2011-2022 走看看