自定义Action和Category

本文介绍了如何在Android中创建并使用自定义Action和Category。为了保证唯一性,通常需要结合应用包名来定义它们。例如,自定义Action为ACTION_SHOW_HELLO,Category为CATEGORY_SHOW_HELLO。在AndroidManifest.xml中,接收此类Intent的组件需要进行相应声明。文中通过实例展示了Activity1如何向Activity2发送包含自定义Action和Category的Intent,使Activity2接收到后显示‘hello’。注意,使用隐式Intent启动组件时,DEFAULT category是必要的,但在显式Intent中并非如此。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有时候,我们会传递给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); // 启动
        }

    }


Activity2

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.




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值