码迷,mamicode.com
首页 > 其他好文 > 详细

Intent系列讲解---Intent简介以及相关属性

时间:2017-01-09 21:43:11      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:技术   extends   dimen   tools   android中   button   highlight   androi   类别   

一.Intent简介

  Intent中文是“意图,意向”,它是Android中四大组件通讯的纽带,Intent负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。Intent不仅可用于应用程序之间,也可用于应用程序内部的Activity/Service之间的交互。因此,可以将Intent理解为不同组件之间通信的“媒介”专门提供组件互相调用的相关信息。

二.启动组件的方法

技术分享

三.Intent的属性(7个)

  技术分享

component(组件):目的组件----Action(动作):用来表现意图的行动----category(类别):用来表现动作的类别----data(数据):表示与动作要操纵的数据

type(数据类型):对于data范例的描写----extras(扩展信息):扩展信息----flags(标志位):期望这个意图的运行模式

 

三.代码实现

(1)ComponentActivity.class

  

/**
 * 直接查找法(通过组件名称)
 */
public class ComponentActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_component);
    }

    /**
     * 直接查找法(通过组件名称)
     * @param view
     */
    public void  componetClick(View view){
        /*Intent intent = new Intent();
        ComponentName componentName = new ComponentName(this,IntentActivity.class);
        intent.setComponent(componentName);*/
        Intent intent = new Intent(this,IntentActivity.class);
        startActivity(intent);
    }

    /**
     * 使用间接法(通过action属性和category属性)
     * @param view
     * 在Activity的清单配置文件中,必须使用默认的类别
     * android.intent.category.DEFAULT
     */
    public void  actionClick(View view){
        /*Intent intent = new Intent();
        intent.setAction("com.langdon.action.MY_ACTION");*/
        Intent intent = new Intent("com.langdon.action.MY_ACTION");
        intent.addCategory("com.langdon.category.MY_CATEGORY");
        startActivity(intent);
    }

    /**
     * data属性,一般与action配合使用(最常见的一种使用方法)
     * type属性,表示数据的类型
     * @param view
     */
    public void  dataClick(View view){
        Intent intent = new Intent();
        //intent.setAction("com.langdon.action.MY_ACTION");
        intent.setAction(Intent.ACTION_VIEW);
        Uri data = Uri.parse("http://www.baidu.com");

        //intent.setData(data);//会把type属性设置为null
        //intent.setType("text/html");//会把data属性设置为null
        //要使用type和data,必须使用以下方法,匹配时必须两者同时匹配才能通过
        intent.setDataAndType(data,"text/html");
        startActivity(intent);
    }

    /**
     * 代码实现Activity的启动模式
     * @param view
     */
    public void  flagClick(View view){
        Intent intent = new Intent(this,FlagActivity.class);
        //设置Activity的启动模式
        //Intent.FLAG_ACTIVITY_NEW_TASK ---在新的任务中启动Activity,如果有,那么在本任务中启动
        //Intent.FLAG_ACTIVITY_CLEAR_TASK ---相当于SingleTask
        //Intent.FLAG_ACTIVITY_CLEAR_TOP ---相当于SingleTop
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        startActivity(intent);

    }


}

  

(2)activity_component.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_componer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.langdon.taiyang.androidtest.Intent.ComponentActivity">

    <Button
        android:id="@+id/bt_component"
        android:text="通过component组件名查找"
        android:onClick="componetClick"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/bt_action"
        android:layout_below="@+id/bt_component"
        android:text="通过action组件名查找"
        android:onClick="actionClick"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/bt_data"
        android:layout_below="@+id/bt_action"
        android:text="通过Data组件名查找"
        android:onClick="dataClick"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/bt_flag"
        android:layout_below="@+id/bt_data"
        android:text="flag启动模式"
        android:onClick="flagClick"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

  

(3)AndroidManifest.xml(IntentActivity---NextActivity---DataActivity都只含有一个TextView,用来在ComponentActivity.class点击按钮显示不同的文字)

 <activity android:name=".Intent.ComponentActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Intent.IntentActivity">
            <intent-filter android:priority="-1">
                <action android:name="com.langdon.action.MY_ACTION" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".Intent.NextActivity">
            <intent-filter android:priority="2">
                <action android:name="com.langdon.action.MY_ACTION" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="com.langdon.category.MY_CATEGORY" />
            </intent-filter>
        </activity>
        <activity android:name=".Intent.DataActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:host="www.baidu.com" android:mimeType="text/html"
                    android:scheme="http" />
            </intent-filter>
        </activity>
        <activity android:name=".Intent.FlagActivity" />

  

运行结果如下:

技术分享

        图1

技术分享

       图2

技术分享

        图3

 

其中图3是点击第三个按钮显示的,其余三个按钮点击后会有一个如图1的textView提

Intent系列讲解---Intent简介以及相关属性

标签:技术   extends   dimen   tools   android中   button   highlight   androi   类别   

原文地址:http://www.cnblogs.com/langdon/p/6266407.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!