Intent属性component,action,category

1、Intent的Component属性需要接受一个ComponentName对象

@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button button=(Button)findViewById(R.id.btn);
		button.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				ComponentName componentName=new ComponentName(ComponentAttr.this, SecondActivity.class);
				Intent intent=new Intent();
				intent.setComponent(componentName);
				startActivity(intent);
			}
		});
	}


 

@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
		TextView textView1=(TextView)findViewById(R.id.tv1);
		ComponentName componentName=getIntent().getComponent();
		textView1.setText("组件包名:"+componentName.getPackageName()
				+"组件类名:"+componentName.getClassName());
	}
<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.intent.ComponentAttr"
            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"></activity>
    </application>

2、Intent的Action、Category属性都是一个普通的字符串,其中Action代表该Intent所要完成的一个抽象“动作”,而Category则用于为Action增加额外的附加类别信息。通常Action属性会和Category属性结合使用。

public final static String CRAZYIT_ACTION="org.example.intent.action.CRAZYIT_ACTION";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_action_attr);
		
		Button button=(Button)findViewById(R.id.btn);
		button.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent();
				intent.setAction(ActionAttr.CRAZYIT_ACTION);
				startActivity(intent);
			}
		});
	}


 

@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
		TextView textView1=(TextView)findViewById(R.id.tv1);
		String action=getIntent().getAction();
		textView1.setText("Action为:"+action);
	}


 

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.intent1.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">
            <intent-filter >
                <action android:name="org.example.intent.action.CRAZYIT_ACTION"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
    </application>


其中<intent-filter.../>元素里通常可包含如下子元素。

0~N个<action.../>子元素。

0~N个<category.../>子元素。

0~1个<data.../>子元素。

需要一提的是:一个Intent对象最多只能包含一个Activity属性,程序调用setAction(Stringstr)来设定Action的属性值;而一个Activity可以有多个Category属性,程序可以调用addCategory(String str)来添加Category属性。当程序创建Intent的时候,创建的Intent属性自动启动属性值为Intent.CATEGORY_DEFAULT常量,其值为“android.intent.category.DEFAULT”,所以,在配置某个Activity属性的时候<categoryandroid:name = “andrid.intent.category.DEFAULT ”>是可以添加到配置文件当中的。

实际上Android内部提供了大量的标准的Action和Category常量。

3、

//action
	final static String CRAZYIT_ACTION="org.example.intent.action.CRAZYIT_ACTION";
	//category
	final static String CRAZYIT_CATEGORY="org.example.intent.category.CRAZYIT_CATEGORY";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button button=(Button)findViewById(R.id.btn);
		button.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent();
				intent.setAction(ActionCateAttr.CRAZYIT_ACTION);
				intent.addCategory(ActionCateAttr.CRAZYIT_CATEGORY);
				startActivity(intent);
			}
		});
	}


 

@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
		TextView textView1=(TextView)findViewById(R.id.tv1);
		String action=getIntent().getAction();
		Set<String> cates=getIntent().getCategories();
		textView1.setText("action:"+action+"  categorys:"+cates);
	}


 

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.intent.ActionCateAttr"
            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="com.example.intent.SecondActivity">
            <intent-filter >
                <action android:name="org.example.intent.action.CRAZYIT_ACTION"/>
                <category android:name="org.example.intent.category.CRAZYIT_CATEGORY"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>
    </application>



 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值