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

Intent见解

时间:2016-05-12 11:46:12      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

今天做了一个很有意思的实验。
三个程序的关系如图
![这里写图片描述](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 kongyeeku@163.com
 * @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 kongyeeku@163.com
 * @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 kongyeeku@163.com
 * @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不在应用列表中

Intent见解

标签:

原文地址:http://blog.csdn.net/qq_21383435/article/details/51371406

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