展会信息港展会大全

Android新手入门教程(十九):使用Intent调用“内置”应用の添加Category属性
来源:互联网   发布日期:2016-01-14 09:34:37   浏览:1328次  

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

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港