Intent见解

今天做了一个很有意思的实验。
三个程序的关系如图
![这里写图片描述](http://img.blog.csdn.net/20160511075941123)

1,先上3个代码

ActionAttr .java文件

package org.crazyit.intent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

/**
 * Description:
 * <br/>site: <a href="http://www.crazyit.org">crazyit.org</a>
 * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee [email protected]
 * @version  1.0
 */
public class ActionAttr extends Activity
{
    public final static String CRAZYIT_ACTION =
        "org.crazyit.intent.action.CRAZYIT_ACTION";

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button bn = (Button) findViewById(R.id.bn);
        // 为bn按钮绑定事件监听器
        bn.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                // 创建Intent对象
                Intent intent = new Intent();
                // 为Intent设置Action属性(属性值就是一个普通字符串)
                intent.setAction(ActionAttr.CRAZYIT_ACTION);
                startActivity(intent);
            }
        });
    }
}

SecondActivity.java文件

/**
 *
 */
package org.crazyit.intent;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee [email protected]
 * @version  1.0
 */
public class SecondActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        EditText show = (EditText) findViewById(R.id.show);
        // 获取该Activity对应的Intent的Action属性
        String action = getIntent().getAction();
        // 显示Action属性
        show.setText("SecondActivity----Action为:" + action);

        Toast.makeText(this, "SecondActivity----Action为:" + action, Toast.LENGTH_SHORT).show();
    }
}

ThirdActivity.java文件

/**
 *
 */
package org.crazyit.intent;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Description:
 * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
 * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee
 * <br/>This program is protected by copyright laws.
 * <br/>Program Name:
 * <br/>Date:
 * @author  Yeeku.H.Lee [email protected]
 * @version  1.0
 */
public class ThirdActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        EditText show = (EditText) findViewById(R.id.show);
        // 获取该Activity对应的Intent的Action属性
        String action = getIntent().getAction();
        // 显示Action属性
        show.setText("ThirdActivity ----Action为:" + action);

        Toast.makeText(this, "ThirdActivity----Action为:" + action, Toast.LENGTH_SHORT).show();

    }
}

布局文件

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    >
<Button
    android:id="@+id/bn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="启动指定Action、默认Category对应的Activity"
    />
</LinearLayout>

second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    >
<EditText
    android:id="@+id/show"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="第二个Activity"
    android:editable="false"
    android:cursorVisible="false"
    />
</LinearLayout>

这里是配置文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="org.crazyit.intent"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="17" />
    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
        <activity android:name=".ActionAttr"
                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=".SecondActivity"
                android:label="@string/app_name">
            <intent-filter>
                <!-- 指定该Activity能响应Action为指定字符串的Intent -->
                <action android:name="org.crazyit.intent.action.CRAZYIT_ACTION" />

            </intent-filter>
        </activity>

    </application>
</manifest> 

第一种情况:

如上的配置文件,直接运行,界面如下

点击第一个按钮后,直接退出程序了。

现在添加一行代码

再次运行

出现结果了。

实验2:把第三个activity加入

运行结果,两个按钮都是一样的结果。

有点疑惑吗?两个都指定了这个

<action android:name="org.crazyit.intent.action.CRAZYIT_ACTION" />

为何只匹配了一个呢?不应该两个都显示吗?

我们来看看这句话的作用;

(1)intent到底发给哪个activity,需要进行三个匹配,一个是action,一个是category,一个是data。

理论上来说,如果intent不指定category,那么无论intent filter的内容是什么都应该是匹配的。但是,如果是implicit intent,Android默认给加上一个CATEGORY_DEFAULT,这样的话如果intent filter中没有android.intent.category.DEFAULT这个category的话,匹配测试就会失败。所以,如果你的 activity支持接收implicit intent的话就一定要在intent filter中加入android.intent.category.DEFAULT。

如果自己定义的某个Activity要通过隐式启动,在AndroidManifast.xm那么必须加上android.intent.category.DEFAULT,否则不起作用

实验3:配置文件改成这样

则变成了这个样子,可以选择启动ThirdActivity还是SecondActivity,两个都能正常启动

说明隐式启动,必须指定,否者activity不在应用列表中

时间: 2024-08-05 17:11:12

Intent见解的相关文章

intent.getExtras()和intent.getStringExtra()

intent.getExtras()返回一个Bundle对象. 看下面代码: Intent intent=new Intent(this,SecondActivity.class); Bundle bundle=new Bundle(); bundle.putString("test", "123"); intent.putExtras(bundle); startActivity(intent); String params=this.getIntent().ge

Intent传参数

Intent 是Android 程序中各组件之间进行交互的一种重要方式,它不仅可以指明当前组 件想要执行的动作,还可以在不同组件之间传递数据.Intent 一般可被用于启动活动.启动 服务.以及发送广播等场景 // A activity调用 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.act

Android之Intent

前言:大家都知道Android程序的实现一般都由四大组件构成: Activity :Android程序实现功能的主体,提供了和客户交互的界面,也提供了和后台交互的功能. Service :是一个没有界面的activity,主要用于后台运行的程序. Broadcast :是当前程序和系统之间通信的工具. ContentProvider :android程序,管理资源的一种工具. 上面这4个组件都需要在manifest里面注册才能够使用,manifest就相当于android程序的大管家.当然光有组

Pro Android学习笔记(十一):了解Intent(中)

Intent的构成 Intent可以带有action,data(由URI表达),extra data(key/value map,键值对),指定的类名(成为component name).一个intent至少携带上述的一个内容. Action.Action名,在上一笔记中已经给出两种例子,一种是系统自带的,如Intent.ACTION_DAIL,一种是开发者通过AndroidManifest.xml进行注册的,在创建intent时给出:Intent intent=new Intent(Strin

Android中Intent的深入理解

(1)Intent提供了一种通用的消息系统,它允许在你的应用程序见传递Intent来执行动作和产生事件,使用Intent可以激活Android应用的三种类型的核心组件:活动Activity.服务Service.广播接受者Broadcast. (2)Intent又分为隐士意图和显示意图. 显示意图:调用intent.setComponent().intent.setClassName()或者intent.setClass()方法明确的制定组件名的Intent为显示意图,显示意图明确的制定要激活哪一

《数据结构编程实验》 2.4.4Calendar个人见解,求指导

题目大意: 制作一个日历系统,输入年份,一些周年纪念日,及服务要求日期,根据要求日期输出,输出重要程度小于发生日期的周年纪念日. 题目地址: UVA  145 个人见解: 纯模拟,在闰年,输出顺序及输出范围可能跨年上有坑.解决了这些后,依旧困了我十多天,一直没过,求指导. 我的程序里先读入周年纪念日,l代表周年纪念日的数量,每读入一个服务日期,调用f()函数,f()函数里先计算每日期相对于当年月号的日期,然后Is()函数判断是否要输出并将要输出移至数组前方,k代表要输出的周年纪念日的数量,最后按

Intent中的四个重要属性——Action、Data、Category、Extras

Intent作为联系各Activity之间的纽带,其作用并不仅仅只限于简单的数据传递.通过其自带的属性,其实可以方便的完成很多较为复杂的操作.例如直接调用拨号功能.直接自动调用合适的程序打开不同类型的文件等等.诸如此类,都可以通过设置Intent属性来完成. Intent主要有以下四个重要属性,它们分别为: Action:Action属性的值为一个字符串,它代表了系统中已经定义了一系列常用的动作.通过setAction()方法或在清单文件AndroidManifest.xml中设置.默认为:DE

intent对于电话和浏览器调用

1.创建xml文件及按钮 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android

ndroid学习笔记-Activity&amp;Intent

界面activity_main.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com