使用intent在活动之间穿梭——使用隐式Intent

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37827702/article/details/77387447
1.在AndroidManifest.xml中
<intent-filter>
    <action android:name="com.sdau.windseeker.activitytest.ACTION_START"/>
    <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
<action>标签中指明了当前活动可以响应com.sdau.windseeker.activitytest.ACTION_START这个action
<category>则包含一些附加信息,更加精确的指明了当前活动能够相应的intent中还有可能带有
category信息
只有<action>,<category>同时能够匹配上intent中指定的action,category时,这个活动才能相应intent

2.protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button1 = (Button) findViewById(R.id.button_1);
    button1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent=new Intent("com.sdau.windseeker.activitytest.ACTION_START");
            intent.addCategory("com.sdau.windseeker.activitytest.MY_CATEGORY");
            startActivity(intent);
        }
    });
}
使用intent函数的另一个构造函数,直接将action参数传了进去,表明响应了 com.sdau.windseeker.activitytest.ACTION_START 这个action活动
com.sdau.windseeker.activitytest.DEFAULT是一种默认的Category,调用 startActivity()这个方法自动将Category添加到Intent中
自己添加 intent.addCategory( "com.sdau.windseeker.activitytest.MY_CATEGORY" );同时需要在 < intent-filter >添加 Category声明
<intent-filter>
    <action android:name="com.sdau.windseeker.activitytest.ACTION_START"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="com.sdau.windseeker.activitytest.MY_CATEGORY"/>
</intent-filter>


猜你喜欢

转载自blog.csdn.net/m0_37827702/article/details/77387447