Intent的显式和隐式跳转

显式

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent);
            }
        });

隐式:

默认的category必须加上

<activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="com.example.fourmajorcomponents.ACTION_START"></action>
                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>
        </activity>
 Intent intent = new Intent("com.example.fourmajorcomponents.ACTION_START");
                startActivity(intent);

每个intent只能指定一个intent,却可以指定多个category

 Intent intent = new Intent("com.example.fourmajorcomponents.ACTION_START");
                intent.addCategory("com.example.fourmajorcomponents.second");
                startActivity(intent);
        <activity android:name=".SecondActivity">
            <intent-filter>
                <action android:name="com.example.fourmajorcomponents.ACTION_START"></action>
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="com.example.fourmajorcomponents.second" />
            </intent-filter>
        </activity>

隐式intent的更多用法
匹配http,音频,视频

<data android:scheme="http">
<data android:scheme="file" android:mimeType="audio/*"/>
<data android:scheme="file" android:mimeType="video/*"/>

第一句是系统内置的一个action,第二句将一个网址解析为一个Uri对象

 web.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.baidu.com"));
                startActivity(intent);
            }
        });

打电话

Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:123456"));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent) ;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容