《Android第一行代码》学习记录003 - 使用Intent切换活动

Intent的使用分为两类:

一、使用显式Intent

显式的Intent就是在实例化Intent时显式提供当前上下文与要跳转活动的上下文,代码如下所示:

Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);

二、使用隐式Intent

隐式Intent并不明确指定要启动哪一个活动,而是通过指定action与category,由系统分析启动哪个活动,步骤如下:

1、在AndroidManifest.xml中注册被启动活动

<activity android:name=".OtherActivity">
    <intent-filter>
        <action android:name="com.xxx.yyy.ACTION_START" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="com.xxx.yyy.zzz" />
    </intent-filter>
</activity>

2、在主调活动中实例化Intent,并为Intent添加category

Intent intent = new Intent("com.xxx.yyy.ACTION_START");
intent.addCategory("com.xxx.yyy.zzz");
startActivity(intent);

隐式Intent的更多用法

通过隐式Intent,使得启动其他应用程序的活动成为可能,如

1、启动浏览器打开网页:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com/"));
startActivity(intent);

2、启动拨号:

Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);

posted on 2015-10-20 11:21  matclone  阅读(219)  评论(0)    收藏  举报

导航