安卓 关于Intent四个属性的总结

来源:互联网 发布:搞怪的淘宝收件人名字 编辑:程序博客网 时间:2024/06/07 05:23

1、Component属性:

Component其实就是 Intent 主要功能属性,见下面代码:

public class ComponentAttr extends Activity{   public void onCreate(Bundle savedInstanceState){    ....       Button btn = (Button)findViewById(R.id.btn);       btn.setonClickListener(new OnClickListener(){            public void onClick(View v){                  ComponentName comp = new ComponentName(ComponentAttr.this,  OtherActivity.class);                  Intent i = new Intent();                  i.setComponent(comp);                  startActivity(i);            }       });   }}


2、Action 属性和 Category属性:

Action属性负责调用Android系统自身的服务。比如 Intent..Action_CALL, 就是调用打电话服务。案例代码:

public class ActionAttr extends Activity{    public final static String CRAZYIT_ACTION = "org.crazyit.intent.action.CRAZYIT_ACTION";    onCreate(...){        ....        Button btn = (Button)findViewById(R.id.btn);        btn.setOnClickListener(new OnClickListener){               public void onClick(View view){                      Intent i = new Intent();                      i.setAction(ActionAttr.CRAZYIT_ACTION);                      startActivity(i);               }        });    }}
因为这里的 Intent 是隐式意图,即 new Intent() 中没有写清调用那两个Activity或Service,所以要靠<intent-filter></intent-filter> 来判别。在AndroidMenifest.xml 中:

<activity name=".ActionAtrr">    <intent-filter>        <action   android:name="org.crzayit.intent.action.CRAZYIT_ACTION" />    <intent-filter></activity>

总结一些 常见的Action 常量和 Category 常量功能。

Action:

ACTION_MAIN  —— android.intent.action.MAIN —— 应用程序入口

ACTION_CALL —— android.intent.action.CALL—— 直接性指定用户打电话

ACTION_SEND —— android.intent.action.SEND —— 向其他人发送数据

Category:

CATEGORY_DEFAULT —— android.intent.category.DEFAULT —— 默认的Category

CATEGORY_LAUNCHER —— android.intent.category.LAUNCHER —— Activity 显示顶级程序


3、Data、Type属性和 intent-filter 属性:

Data属性用来向 Action 属性提供操作数据。Data 属性接受一个 URI 对象,这个URI 对象通常以字符串形式来表示:

比如:content :// com.android.contacts/contacts/1

表示:contant前缀:表示该数据类型为“联系人信息”

//com.android.contacts/contacts/1: 表示操作_id 为1的联系人数据类型来启动特定的应用程序,并对指定数据执行相应的操作。



下面介绍在Intent中 Action 和 Data属性组合的一些形式:

btn.setOnClickListener(new OnClickListener(){    public void onClick(View view){        Intent intent = new Intent();        intent.setAction(Intent.ACTION_CALL);        intent.setData(Uri.parse("tel:138XXXXXX"));        startActivity(intent);    }})



















0 0