• Intent 意图


    1.Intent作用

    Intent是一个将要执行的动作的抽象的描述,解决Android应用的各项组件之间的通讯,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。intent主要用来启动activity或者service(并携带需要传递的参数信息)。

    2.Intent形式

    意图包括:Action(动作),Category(附加信息),Data(数据,具体内容),Tpye(类型)等等,举个例子,说白了意图就是启动一个组件的的完整的动作信息,就像打人,打就是Action动作,人就是Data内容,而Type就是类型,打什么人呢?打坏人,type就是坏指的类型,只有这些信息全了才能执行一个完整的意图,当然还有一些信息,比如scheme就是URI类型的数据的前缀,就像这个例子当中的sms:,还有host主机名,path路径等。

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

    • 调用Intent.setComponent()或Intent.setClass()方法明确指定了组件名的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.
    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.
    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.设置IntentFilter当前应用打开(应用多选)

    <activity  
        android:name="com.example.player.MainActivity"  
        android:label="@string/app_name" >  
        <intent-filter>  
            <action android:name="android.intent.action.MAIN" />  
      
            <category android:name="android.intent.category.LAUNCHER" />  
        </intent-filter>  
          
        <!-- 指定action, data类型为video, category为default,设置完成之后,当打开video文件时,本应用作为可选打开软件 -->  
        <intent-filter>  
            <action android:name="android.intent.action.VIEW" />  
      
            <data android:mimeType="video/*" />  
      
            <category android:name="android.intent.category.DEFAULT" />  
        </intent-filter>  
    </activity>

    使用隐式意图启动系统短信,并给指定号码发送信息

    /** 
         * 隐式意图的方法启动系统短信 
         *  
         * 简单概括就是: 意图包括:Action(动作),Category(附加信息),Data(数据,具体内容),Tpye(类型)等等,举个例子, 
         * 说白了意图就是启动一个组件的的完整的动作信息 
         * ,就像打人,打就是Action动作,人就是Data内容,而Type就是类型,打什么人呢?打坏人,type就是坏指的类型 
         * ,只有这些信息全了才能执行一个完整的意图 
         * ,当然还有一些信息,比如scheme就是URI类型的数据的前缀,就像这个例子当中的sms:,还有host主机名,path路径等 
         *  
         * @param view 
         */  
        public void startOne(View view) {  
            Intent intent = new Intent();  
            intent.setAction("android.intent.action.SENDTO");// 发送信息的动作  
            intent.addCategory("android.intent.category.DEFAULT");// 附加信息  
            intent.setData(Uri.parse("sms:10086"));// 具体的数据,发送给10086  
            startActivity(intent);  
        }
    

      

    • 自定义的隐式意图调用:
      • 首先是先在清单文件中进行注册:
    <?xml version="1.0" encoding="utf-8"?>  
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
        package="net.loonggg.intent"  
        android:versionCode="1"  
        android:versionName="1.0" >  
      
        <uses-sdk  
            android:minSdkVersion="8"  
            android:targetSdkVersion="17" />  
      
        <application  
            android:allowBackup="true"  
            android:icon="@drawable/ic_launcher"  
            android:label="@string/app_name"  
            android:theme="@style/AppTheme" >  
            <activity  
                android:name="net.loonggg.intent.MainActivity"  
                android:label="@string/app_name" >  
                <intent-filter>  
                    <action android:name="android.intent.action.MAIN" />  
      
                    <category android:name="android.intent.category.LAUNCHER" />  
                </intent-filter>  
            </activity>  
            <activity android:name="net.loonggg.intent.SecondActivity" >  
                <intent-filter>  
      
                    <!-- 自定义的动作 -->  
                    <action android:name="net.loonggg.xxx" />  
                    <!-- 自定义的scheme和host -->  
                    <data  
                        android:host="www.baidu.com"  
                        android:path="/person"  
                        android:scheme="loonggg" />  
                    <!-- 自定义的类型 -->  
                    <data android:mimeType="person/people" />  
                    <!-- 附加信息 -->  
                    <category android:name="android.intent.category.DEFAULT" />  
                </intent-filter>  
            </activity>  
        </application>  
      
    </manifest>
    • 自定义设置的Activity:

      

    <activity android:name="net.loonggg.intent.SecondActivity" >  
               <intent-filter>  
      
                   <!-- 自定义的动作 -->  
                   <action android:name="net.loonggg.xxx" />  
                   <!-- 自定义的scheme和host -->  
                   <data  
                       android:host="www.baidu.com"  
                       android:path="/person"  
                       android:scheme="loonggg" />  
                   <!-- 自定义的类型 -->  
                   <data android:mimeType="person/people" />  
                   <!-- 附加信息 -->  
                   <category android:name="android.intent.category.DEFAULT" />  
               </intent-filter>  
           </activity>
    • 代码调用自定义
    /** 
         * 通过自定义的隐式意图启动 
         *  
         * @param view 
         */  
        public void startTwo(View view) {  
            Intent intent = new Intent();  
            intent.setAction("net.loonggg.xxx");  
            intent.addCategory("android.intent.category.DEFAULT");  
            intent.setDataAndType(Uri.parse("loonggg://www.baidu.com/person"),  
                    "person/people");  
            startActivity(intent);  
        }

    注意:

    intent.setData(data)和intent.setType(type)注意这两个方法会互相清除,意思就是:如果先设置setData(data)后设置setType(type),那么后设置的setType(type)会把前面setData(data)设置的内容清除掉,而且会报错,反之一样,所以如果既要设置类型与数据,那么使用setDataAndType(data,type)这个方法。

  • 相关阅读:
    Linux驱动之异常处理体系结构简析
    Linux驱动之按键驱动编写(查询方式)
    Linux驱动之LED驱动编写
    Linux驱动之建立一个hello模块
    Linux驱动之内核加载模块过程分析
    制作根文件系统之制作根文件系统步骤详解
    制作根文件系统之Busybox init进程的启动过程分析
    制作根文件系统之内核如何启动init进程
    制作根文件系统之内核挂接文件系统步骤分析
    Linux移植之tag参数列表解析过程分析
  • 原文地址:https://www.cnblogs.com/deny-66/p/5589505.html
Copyright © 2020-2023  润新知