Application Fundamentals(5)

Declaring component capabilities

As discussed above, in Activating Components, you can use an Intent to start activities, services, and broadcast receivers.
如上面讨论的,在激活组件方面,你可以使用一个Intent来启动activities, services, 和 broadcast receivers.

You can do so by explicitly naming the target component (using the component class name) in the intent.
你可以显式的命名目标组件来使用intent激活他们(使用组件的类名)。

However, the real power of intents lies in the concept of implicit intents.
然而,intents的真正力量在于隐式intents的概念

An implicit intent simply describes the type of action to perform (and, optionally, the data upon which you’d like to perform the action) and allows the system to find a component on the device that can perform the action and start it.
一个隐式的intent描述了一种要执行的动作(并且你可以选择想要执行这个动作的数据),这就准许系统在设备上找到一种能够执行这个动作的组件并且启动它。

If there are multiple components that can perform the action described by the intent, then the user selects which one to use.
如果有多种组件可以执行由这个intent定义的动作,那就交由用户来选择使用哪个组件。

The way the system identifies the components that can respond to an intent is by comparing the intent received to the intent filters provided in the manifest file of other apps on the device.
系统确定哪个组件可以响应一个intent的方式,就是将接收到的intent 和在设备上的其他应用的清单文件里面提供的intent过滤器进行比较。

When you declare an activity in your app’s manifest, you can optionally include intent filters that declare the capabilities of the activity so it can respond to intents from other apps. You can declare an intent filter for your component by adding an element as a child of the component’s declaration element.
当你在你的应用清单文件里面声明一个activity的时候,你可以随意的包含那些说明你的activity功能的intent filters,以至于它可以回应来自于其他应用的intents。你可以通过添加一个 作为组件声明元素的子元素,来为你的组件声明一个意图过滤器。

For example, if you’ve built an email app with an activity for composing a new email, you can declare an intent filter to respond to “send” intents (in order to send a new email) like this:
比如,如果你构建了一个带有可以编辑新邮件的
activity的邮箱应用,你可以像下面这样声明一个意图过滤器来回应“send”的意图(send意味着发送一封新邮件):

<manifest ... >
...
<application ... >
<activity android:name="com.example.project.ComposeEmailActivity">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<data android:type="*/*" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>

Then, if another app creates an intent with the ACTION_SEND action and pass it to startActivity(), the system may start your activity so the user can draft and send an email.
如果另一个应用创建了一个携带ACTION_SEND动作的意图并且用startActivity()来发送它,系统可能就会启动你的activity以至于用户可以拟稿并且发送一封邮件。

For more about creating intent filters, see the Intents and Intent Filters document.
了解更多关于创建意图过滤器的知识,可以查看
Intents and Intent Filters文档。

Declaring app requirements

There are a variety of devices powered by Android and not all of them provide the same features and capabilities.
有各种各样的安卓系统设备,但并不是所有设备都提供了相同的特性和功能。

In order to prevent your app from being installed on devices that lack features needed by your app, it’s important that you clearly define a profile for the types of devices your app supports by declaring device and software requirements in your manifest file.
为了防止你的应用安装在那些对于你的应用功能缺失的设备上,很重要的一点是,通过在manifest文件中声明设备和软件需求,明确定义应用程序支持的设备类型。

Most of these declarations are informational only and the system does not read them, but external services such as Google Play do read them in order to provide filtering for users when they search for apps from their device.
这些声明中的大部分都只是信息而已,系统不会去读取他们,但是外部服务比如谷歌市场会去读取他们,为了让用户从设备中搜索应用时提供过滤功能。

For example, if your app requires a camera and uses APIs introduced in Android 2.1 (API Level 7), you should declare these as requirements in your manifest file like this:
比如,如果你的应用需要使用照相机并且使用了在2.1引入的api,你应该像下面这样,在你的清单文件中声明这些需求:
(在你的清单文件中按照要求声明)???

<manifest ... >
<uses-feature android:name="android.hardware.camera.any"
android:required="true" />
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="19" />
...
</manifest>

Now, devices that do not have a camera and have an Android version lower than 2.1 cannot install your app from Google Play.
这样的话,没有照相机以及安卓版本低于2.1的设备就不能从谷歌市场中安装你的应用。

However, you can also declare that your app uses the camera, but does not require it. In that case, your app must set the required attribute to “false” and check at runtime whether the device has a camera and disable any camera features as appropriate.
然而,你也可以让你的应用使用照相机,但是声明的时候不去请求使用照相机。在这种情况下,你的应用必须设置required属性为false,然后在运行时检查设备是否有照相机,并在适当的时候禁用任何相机功能。

More information about how you can manage your app’s compatibility with different devices is provided in the Device Compatibility document.
更多关于如何管理你应用在不同设备上兼容性的信息,已经在Device Compatibility文档中提供了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值