Intent 和 IntentFilter

1、Android应用程序包含三个重要的组件:Activity、Service、BroadcastReceiver。

2、Intent的Componet属性

(1)

//创建一个ComponentName对象

ComponentName comp = new ComponentName(ComponentAttr.this, SecondActivity.class);

Intent intent = new Intent();

intent.setComponent(comp);

(2)

Intent intent = new Intent(ComponentAttr.this, SecondActivity.class);

上面(1)和(2)是等价的,都可以启动另一个Activity。

3、Action、Category属性与intent-filter配置

看一个例子:

第一个Activity

package com.sailang.intenttest;

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;

public class ActionAttr extends Activity
{
	public static final String CRAZYIT_ACTION = "helloworld";
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		Button bn = (Button)findViewById(R.id.bn);
		
		bn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent();
				intent.setAction(ActionAttr.CRAZYIT_ACTION);
				startActivity(intent);
			}
		});
	}
}

第二个Activity:

package com.sailang.intenttest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;

public class SecondActivity extends Activity
{
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.second);
		
		EditText show = (EditText)findViewById(R.id.show);
		
		String action = getIntent().getAction();
		
		show.setText("Action为:" + action);
	}
}

Manifest文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sailang.intenttest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="4" />

    <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>
                <action android:name="helloworld" />
                
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

 

 

你可能感兴趣的:(Intent 和 IntentFilter)