有时候,我们会传递给Intent自定义的Action和Category, Action和Category都是一个字符串,所以可以是任意的,但是为了确保唯一性,我们需要为其加上当包名,如果当前的包名为com.example.intentsample, Action的名字为ACTION_SHOW_HELLO, 那么Androidmanifest.xml中,需要为接收此Action的组件加上如下声明
<action android:name="com.example.intentsample.ACTION_SHOW_HELLO"/
category的定义类似于action的定义
下面的代码让Activity1给Activity2传递一个Intent, 这个Intent包含有自定义的Action和Category, Activity2接收到后,显示“hello"字串
Activity1
public class Activity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1_layout);
}
/*
* (non-Javadoc)
* @see android.app.Activity#onStart()
*/
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Intent intent = new Intent();
intent.setAction(Activity2.ACTION_SHOW_HELLO);// 设置Action
intent.addCategory(Activity2.CATEGORY_SHOW_HELLO); // 添加Category,满足CATEGORY_SHOW_HELLO的组件会收到这个Intent
if (intent.resolveActivity(getPackageManager()) != null) { // 判断是否有满足这个intent的组件
startActivity(intent); // 启动
}
}
public class Activity2 extends Activity {
public static final String ACTION_SHOW_HELLO_ACTIVITY2 = "com.example.intentsample.Action_SHOW_HELLO<span style="font-family: Arial, Helvetica, sans-serif;">";</span>
public static final String CAT_SHOW = "com.example.intentsample.CATEGORY_SHOW_HELLO";
/*
* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2_main);
Intent intent = getIntent();//取得启动Activity2的Intent
if (intent.getAction().equals(ACTION_SHOW_HELLO_ACTIVITY2)
&& intent.getCategories().contains(<span style="font-family: Arial, Helvetica, sans-serif;">CATEGORY_SHOW_HELLO</span><span style="font-family: Arial, Helvetica, sans-serif;">)) { //当Action和Category匹配时显示hello字符串</span>
((TextView) findViewById(R.id.hello_txt)).setText("hello");
}
}
}
AndroidManifest.xml中加入intent-filter
<activity
android:name="com.example.intentsample.Activity1"
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.intentsample.Activity2" >
<intent-filter >
<action android:name="com.example.intentsample.ACTION_SHOW_HELLO"/>
<category android:name="com.example.intentsample.CATEGORY_SHOW_HELLO"/>
<!-- 必须加入"android.intent.category.DEFAULT"才能让startActivity()函数找得到这个Activity2 -->
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
注意,必须加入
<category android:name="android.intent.category.DEFAULT"/>
否则,即使有声明
<category android:name="com.example.intentsample.CATEGORY_SHOW_HELLO"/>
还是无法启动Activity2,因为startActivity()无法在组件列表中找到这个Activity2, Google Android API文档里说的很清楚
we support the DEFAULT category to allow the activity to be launched without explicitly specifying its component. 当我们通过Implicit Intent启动组件时, DEFAULT category是必须的,换句话说,当使用Explicit Intent启动组件的时候,因为startActivity()可以找得到这个Activity,所以不需要加DEFAULT category.