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

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>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值