Android新手入門教程(十九):使用Intent調用“內建”應用の添加Category屬性

來源:互聯網
上載者:User

通過使用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.amazon.com"));
        // 注意這句代碼
        i.addCategory("net.learn2develop.Apps");
        startActivity(Intent.createChooser(i, "Open URL using..."));
在以上的代碼中,我們使用addCategory()方法為Intent對象添加Category屬性。如果遺漏了addCategory()這句,之前的代碼仍然能夠調用MyBrowserActivity,因為它仍然匹配了預設的Category:android.intent.category.DEFAULT。


但是,如果指定了一個並沒有在Intent-Filter中定義的Category,那麼,將不會有Activity被調用:


[java] Intent i = new 
         Intent(android.content.Intent.ACTION_VIEW, 
             Uri.parse("http://www.amazon.com")); 
 // i.addCategory("net.learn2develop.Apps");  
 
 // 這個category不匹配Intent-Filter中的任何category  
 i.addCategory("net.learn2develop.OtherApps"); 
 startActivity(Intent.createChooser(i, "Open URL using...")); 
        Intent i = new
                Intent(android.content.Intent.ACTION_VIEW,
                    Uri.parse("http://www.amazon.com"));
        // i.addCategory("net.learn2develop.Apps");

        // 這個category不匹配Intent-Filter中的任何category
        i.addCategory("net.learn2develop.OtherApps");
        startActivity(Intent.createChooser(i, "Open URL using..."));net.learn2develop.OtherApps,不匹配Intent-Filter中的任何一個Category,所以,運行以上代碼的時候,將會拋出一個運行時異常。

但是,如果在AndroidManifest.xml中添加如下代碼,之前的代碼就可以運行了:


[java] <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" /> 
        <!--  添加這句代碼 --> 
        <category android:name="net.learn2develop.OtherApps" /> 
        <data android:scheme="http" /> 
    </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" />
                <!--  添加這句代碼 -->
                <category android:name="net.learn2develop.OtherApps" />
                <data android:scheme="http" />
            </intent-filter>
        </activity>
也可以為Intent對象添加多重Category屬性,舉個例子:


[java] Intent i = new 
        Intent(android.content.Intent.ACTION_VIEW, 
            Uri.parse("http://www.amazon.com")); 
// 多重的Category  
i.addCategory("net.learn2develop.Apps"); 
i.addCategory("net.learn2develop.OtherApps"); 
i.addCategory("net.learn2develop.SomeOtherApps"); 
startActivity(Intent.createChooser(i, "Open URL using...")); 
        Intent i = new
                Intent(android.content.Intent.ACTION_VIEW,
                    Uri.parse("http://www.amazon.com"));
        // 多重的Category
        i.addCategory("net.learn2develop.Apps");
        i.addCategory("net.learn2develop.OtherApps");
        i.addCategory("net.learn2develop.SomeOtherApps");
        startActivity(Intent.createChooser(i, "Open URL using..."));因為Intent-Filter中並沒有定義net.learn2develop.SomeOtherApps這個Category,以上的代碼將不會調用MyBrowserActivity。如果想要解決這個問題,那麼,在目標Activity被調用之前,添加到Intent對象中的所有Category屬性都必須全部地被定義在Intent-Filter中。
 

摘自  horsttnann的專欄
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.