当前位置:操作系统 > 安卓/Android >>

Android新手入门教程(十九):使用Intent调用“内置”应用の添加Category属性

通过使用Intent-Filter中的<category>元素,我们可以把activities进行分组。假设已经在AndroidManifest.xml中添加了<category>元素:


[java] <?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="net.learn2develop.Intents" 
    android:versionCode="1" 
    android:versionName="1.0" > 
 
    <uses-sdk android:minSdkVersion="14" /> 
 
    <uses-permission android:name="android.permission.CALL_PHONE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
 
    <application 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" > 
        <activity 
            android:name=".IntentsActivity" 
            android:label="@string/app_name" > 
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" /> 
 
                <category android:name="android.intent.category.LAUNCHER" /> 
            </intent-filter> 
        </activity> 
        <activity 
            android:name=".MyBrowserActivity" 
            android:label="@string/app_name" > 
            <intent-filter> 
                <action android:name="android.intent.action.VIEW" /> 
                <action android:name="net.learn2develop.MyBrowser" /> 
 
                <category android:name="android.intent.category.DEFAULT" /> 
                <!-- 注意这句代码 --> 
                <category android:name="net.learn2develop.Apps" /> 
 
                <data android:scheme="http" /> 
            </intent-filter> 
        </activity> 
    </application> 
 
</manifest> 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.learn2develop.Intents"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="14" />

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".IntentsActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MyBrowserActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <action android:name="net.learn2develop.MyBrowser" />

                <category android:name="android.intent.category.DEFAULT" />
                <!-- 注意这句代码 -->
                <category android:name="net.learn2develop.Apps" />

                <data android:scheme="http" />
            </intent-filter>
        </activity>
    </application>

</manifest>在这种情况下,以下的代码将直接调用MyBrowserActivity:


[java] Intent i = new 
        Intent(android.content.Intent.ACTION_VIEW, 
            Uri.parse("http://www.amazon.com")); 
nbsp;// 注意这句代码  
i.addCategory("net.learn2develop.Apps"); 
startActivity(Intent.createChooser(i, "Open URL using...")); 
        Intent i = new
                Intent(android.content.Intent.ACTION_VIEW,
                    Uri.parse("http://www.amaz

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,