今天仍在学习Intent
Intent其实就是两个Activity之间的纽带,它实现了从一个活动跳转到另一个活动,其中也有一些功能,比如说数据的传递。
Intent分为显式和隐式:
- 显示Intent
比如说点击一个按钮切换到另一个事件,可在button的OnClick()方法内写入如下代码:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
- 隐式Intent
step1:在要跳转到的活动的标签中加入以下代码(相当于指明了当前的活动能够匹配的action和category):
<action android:name="com.example.activitytest.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
step2:在FirstActivity中的onClick()事件中添加如下代码:
Intent intent = new Intent("com.example.activitytest.ACTION_START");
startActivity(intent);
另外还要注意一点,每个Intent中只能指定一个action,却能制定多个category。
代码如下(已经实例化了一个intent对象):
intent.addCategory("com.example,activitytest.MY_CATEGORY");
别忘了还要在SecondActivity的中添加:
<category android:name="com.example.activitytest.MY_CATEGORY"/>
- 更多用法
利用intent跳转至百度的网页
onClick()中添加:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);