Intent

作用

android中的通信工具。可以看成包裹

六大属性

ComponentName Action Category Data Type Extra
6大属性中,前面5个均用于指定本intent用于启动哪个组件,ComponentName直接指定组件的名称,后4个指定类型,只要符合此类型条件的组件均可响应此Intnet。前者称为显式Intnet,后者称为隐式intent。前者一般用于启动自身程序的其它组件,后者一用于启动其它程序的组件(包括系统组件)。
intent的属性

action

Dail:
Intent intent = new Intent();
                intent.setAction(Intent.ACTION_DIAL);

                intent.setData(Uri.parse("tel:"+etNumber.getText().toString()));
                startActivity(intent);
Call

(需要在manifest中添加权限

<uses-permission android:name="android.permission.CALL_PHONE" />

):

Intent intent = new Intent();
                intent.setAction(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:15764230137"));
                startActivity(intent);
SendTO
View
隐式启动activity

这些就放在一起了

 private void sendSMS() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("smsto:15764230137"));
        intent.putExtra("sms_body", "你还好吗??");
        startActivity(intent);
    }

    private void web() {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("http://www.baidu.com"));
        startActivity(intent);
    }

    private void hide() {
        Intent intent = new Intent("com.lingzhuo.test2.SecondActivity");
        intent.putExtra(Config.MAIN_TO_SECOND, etNumber.getText().toString());
        startActivity(intent);
    }

data

用于传递数据
Intent的Data属性是执行动作的URI和MIME类型,不同的Action有不同的Data数据指定。比如:ACTION_EDIT Action应该和要编辑的文档URI Data匹配,ACTION_VIEW应用应该和要显示的URI匹配。

Extra

用于传递数据
Intent的Extra属性是添加一些组件的附加信息。比如,如果我们要通过一个Activity来发送一个Email,就可以通过Extra属性来添加subject和body。
下面的例子在第一个Activity的EditText输入用户名,该年龄保存在Intent的Extras属性中。当单击Button时,会在第二个Activity中显示用户名。

//发送端
Intent intent = new Intent("com.lingzhuo.test2.SecondActivity");
        intent.putExtra(Config.MAIN_TO_SECOND, etNumber.getText().toString());
        startActivity(intent);

//接收端
Intent intent = getIntent();
        String s = intent.getStringExtra(Config.MAIN_TO_SECOND);
        tvSecond.setText(s);

category

Intent中的Category属性是一个执行动作Action的附加信息。比如:CATEGORY_HOME则表示放回到Home界面,ALTERNATIVE_CATEGORY表示当前的Intent是一系列的可选动作中的一个。下表是SDK文档中关于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

Intent的Type属性显式指定Intent的数据类型(MIME)。一般Intent的数据类型能够根据数据本身进行判定,但是通过设置这个属性,可以强制采用显式指定的类型而不再进行推导
– 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

Intent的Compent属性指定Intent的的目标组件的类名称。通常 Android会根据Intent 中包含的其它属性的信息,比如action、data/type、category进行查找,最终找到一个与之匹配的目标组件。但是,如果 component这个属性有指定的话,将直接使用它指定的组件,而不再执行上述查找过程。指定了这个属性以后,Intent的其它所有属性都是可选的。
– 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值