安卓成长记(七)

安卓成长记(七)

下面的是书上第五章的内容,设计的是intent和Intentfilter的内容

Intent不是组件,却胜似组件

从activity启动四个组件都是用的Intent

intent组件有一下几个属性:

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed. The primary pieces of information in an intent are:

  • action – The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.

  • data – The data to operate on, such as a person record in the contacts database, expressed as a Uri.

In addition to these primary attributes, there are a number of secondary attributes that you can also include with an intent:

  • category – Gives additional information about the action to execute. For example, CATEGORY_LAUNCHER means it should appear in the Launcher as a top-level application, while CATEGORY_ALTERNATIVE means it should be included in a list of alternative actions the user can perform on a piece of data.

  • type – Specifies an explicit type (a MIME type) of the intent data. Normally the type is inferred from the data itself. By setting this attribute, you disable that evaluation and force an explicit type.

  • component – Specifies an explicit name of a component class to use for the intent. Normally this is determined by looking at the other information in the intent (the action, data/type, and categories) and matching that with a component that can handle it. If this attribute is set then none of the evaluation is performed, and this component is used exactly as is. By specifying this attribute, all of the other Intent attributes become optional.

  • extras – This is a Bundle of any additional information. This can be used to provide extended information to the component. For example, if we have a action to send an e-mail message, we could also include extra pieces of data here to supply a subject, body, etc.

一堆英文也看不懂。
其中Component是明确指定要自动的目标组件
extras是携带的数据。
指定Component的Intent已经明确指定了要启动那个组件。

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)
            {
                // 创建一个ComponentName对象
                //这简直是没事儿干了。新建一个Compontent对象
                //为其指定那个在Intent指定就可以的东西,
                //ComponentName好几个这个的重载方法
                ComponentName comp = new ComponentName(MainActivity.this,
                    SecondActivity.class);
                Intent intent = new Intent();
                // 为Intent设置Component属性
                //时候Intent调用setComponent()这个方法。
                intent.setComponent(comp);
                startActivity(intent);
            }
        });
    }

能写出上面的代码的程序员真是太闲了对不对。

上面说了,Action重要。category是action的附加属性,通常用于给action增加附加信息

这两个值的内容都是两个字符串

其实指定action属性就是指定字符串

public class MainActivity 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对象并为其指定action属性
                Intent intent = new Intent();
                // 为Intent设置Action属性(属性值就是一个普通字符串)
                //不要被下面这一串吓到:
                //"org.crazyit.intent.action.CRAZYIT_ACTION";
                //不过是个长了一点儿的字符串而已。你写aa.bb.cc.dd没有关系。
                intent.setAction(MainActivity.CRAZYIT_ACTION);
                startActivity(intent);
            }
        });
    }
}

怎么确定谁是被响应的activity呢?

被响应的Activity中有intentFilter啊!在manifest中指定这个就行了。

< intent-filter>元素里能指定多个action属性,多个category属性。

对于上面的程序,能响应上面的intent的manifest的配置文件:
中一定要有这样一句话:

< action android:name=”org.crayit.intent.action.CRAZYIT_ACTION”

这样就能响应上面的那个intent了。

另外注意,一个activity可以包含多个action,但是一个intent只能包含一个action。

说白了也就是一个intent只能其中一种(或者干脆一个,但是一个有点不对,存在多个activity可以响应一个intent的情况)

但是一个intent可以指定多个category属性,category就是个拾遗,看action哪里不完整了上去补补。

如果不指定category也没关系,intent有默认的category属性DEFAULT。

为intent设置action属性:

public void onClick(View arg0)
            {
                // 创建Intent对象
                Intent intent = new Intent();
                // 为Intent设置Action属性(属性值就是一个普通字符串)
                //intent.setAction(这里面只要是一个字符串就好)
                intent.setAction(MainActivity.CRAZYIT_ACTION);
                startActivity(intent);
            }

其实这个intent的action是能拿出来的

public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
        EditText show = (EditText) findViewById(R.id.show);
        // 获取该Activity对应的Intent的Action属性
        //先利用getIntent()获取一个Intent
        //之后利用这个Intent的getAction()方法获取……action
        //无非就是个string而已。
        String action = getIntent().getAction();
        // 显示Action属性
        show.setText("Action为:" + action);
    }

这个category吧很有意思,不指定没事,但是指定了,被启动的activity就得有这个东西。

public void onClick(View arg0)
            {
                Intent intent = new Intent();
                // 设置Action属性
                intent.setAction(MainActivity.CRAZYIT_ACTION);
                // 添加Category属性
                //我在这里设置了category属性
                intent.addCategory(MainActivity.CRAZYIT_CATEGORY);
                startActivity(intent);
            }

于是个被启动的activity的配置文件就得有包含action和category的属性。
而且注意一个intent可以有几个category属性

<activity android:name=".SecondActivity"
                  android:label="@string/app_name">
            <intent-filter>
                
                <action android:name="org.crazyit.intent.action.CRAZYIT_ACTION" />
                
                <category android:name="org.crazyit.intent.category.CRAZYIT_CATEGORY" />
                
                <category android:name="android.intent.category.DEFAULT" />
            intent-filter>
        activity>

action和category搭配在一起,有个很大招的功能
就是开启系统的Activity

其实这里就显示了intent的好处,intent里面指定了action就能打开系统应用的某个界面
系统维护了大量的action和category的常量,比如:

ACTION_MAIN 就是应用程序的入口
ACTION_DIAL就是显示拨号面板
ACTION_SEND发送短信面板

最常用的一个搭配是返回Home桌面

intent.setAction(Intent.ACTION_MIAN)
intent.addCategory(Intent.category.CATEGORY_HOME)
注意category用的是add不是set

现在还还剩下data和Type属性

这俩属性还能干啥呢?其实也就是增加了一点的限定条件,跟category也差不多。

data属性接受一个Uri对象,type属性用于指定该Data属性所指定的Uri的MIME类型。

MIME可以使任何类型,符合abc/xyz就行了。

不过我真心不知道这俩属性的存在意义

public void overrideType(View source)
    {
        Intent intent = new Intent();
        // 先为Intent设置Type属性
        intent.setType("abc/xyz");
        // 再为Intent设置Data属性,覆盖Type属性
        //就注意一下setData(Uri.parse())就行了
        intent.setData(Uri.parse("lee://www.fkjava.org:8888/test"));
        Toast.makeText(this, intent.toString(), Toast.LENGTH_LONG).show();
    }

mainfest文件

<activity
            android:icon="@drawable/ic_type"
            android:name=".SchemeHostPortPathTypeActivity"
            android:label="指定scheme、host、port、path、type的Activity">
            <intent-filter>
                <action android:name="xx"/>
                //在intent filter里面要指定category属性,data还有type属性。 
                <category android:name="android.intent.category.DEFAULT" />
                
                <data android:scheme="lee"
                      android:host="www.fkjava.org"
                      android:port="8888"
                      android:path="/mypath"
                      android:mimeType="abc/xyz"/>
            intent-filter>
        activity>

不过在打开系统的特定界面的时候,action和data搭配比较有用

关于intent里面那些属性,就先到这里把

记住都是为了打开另一个组件,另一个组件加过滤器。
另一个组件可以使系统组件什么的。

你可能感兴趣的:(安卓成长记(七))