Android M Developer Preview - API Preview(一)

API Overview

The M Developer Preview gives you an advance look at the upcoming release for the Android platform, which offers new features for users and app developers. This document provides an introduction to the most notable APIs.

即将到来的Android平台的释放版本将会为用户以及App开发者提供新的功能特性,M Developer Preview则为此提供一个预览。本文档将会向大家介绍大多数值得注意的APIs。

The M Developer Preview is intended for developer early adopters and testers. If you are interested in influencing the direction of the Android framework, give the M Developer Preview a try and send us your feedback!

M Developer Preview面向前期的开发人员和测试人员。如果你有意直接作用于Android frameword,不妨尝试下M Developer Preview,并向我们发送你的反馈!

Caution: Do not publish apps that use the M Developer Preview to the Google Play store.

警告:不要推送那些使用了M Developer Preview的应用到谷歌应用商店上去。

Note: This document often refers to classes and methods that do not yet have reference material available on developer.android.com. These API elements are formatted in code style in this document (without hyperlinks). For the preliminary API documentation for these elements, download the preview reference.

注意:本文档会经常提到在developer.android.com上还没有对应的可用参考材料的类和方法。本文档中这些API标记都是代码样式的格式(而不是超链接)。这些标签对应的早期的API文档,在preview reference中可以下载。

Important behavior changes重要的行为更改

If you have previously published an app for Android, be aware that your app might be affected by changes in the platform.

如果你之前已经推送了一款Android平台下的App,你会发觉你的App可能会受到新平台中行为的更改而带来的影响。

Please see Behavior Changes for complete information.

请阅读行为更改章节获取完整的信息。

App Linking App 链接

This preview enhances Android’s intent system by providing more powerful app linking. This feature allows you to associate an app with a web domain you own. Based on this association, the platform can determine the default app to use to handle a particular web link and skip prompting users to select an app. To learn how to implement this feature, see App Linking.

该预览通过提供更多有效的App链接改善了Android的意图系统。该特性允许你通过你自己的一个web域名来关联某个app。在此关联的基础上,新平台会确定用于处理某一特定web链接的默认App,并跳过提示用户选择一款App的步骤。查阅App Linking,以学习如何实现该特性。

Auto Backup for Apps Apps自动备份

The system now performs automatic full data backup and restore for apps. For the duration of the M Developer Preview program, all apps are backed up, independent of which SDK version they target. After the final M SDK release, your app must target M to enable this behavior; you do not need to add any additional code. If users delete their Google accounts, their backup data is deleted as well. To learn how this feature works and how to configure what to back up on the file system, see Auto Backup for Apps.

现在的Android系统平台会自动地对App进行所有数据的备份和还原。M Developer Preview 程序运行期间,所有的App都会被备份,且与各个App的目标SDK版本无关。M SDK最终的预览版释放之后,你的App必须target到M以激活该行为;无须添加任何额外的代码。如果用户删除了他们的Google账户,他们的数据备份也会被删除。学习该特性的工作原理及如何设置将被被备份到文件系统上的数据,请查看Auto Backup for Apps。

Authentication 身份验证

This preview offers new APIs to let you authenticate users by using their fingerprint scans on supported devices, and check how recently the user was last authenticated using a device unlocking mechanism (such as a lockscreen password). Use these APIs in conjunction with the Android Keystore system.

为了能够让你在支持指纹识别的设备上通过指纹扫描来进行用户身份验证,同时检查最近一次进行身份验证的用户是如何使用设备解锁机制的(例如锁屏密码),该预览版提供了一些新的APIs,结合Android Keystore system章节来使用这些API吧。

Fingerprint Authentication 指纹认证

To authenticate users via fingerprint scan, get an instance of the new FingerprintManager class and call the authenticate() method. Your app must be running on a compatible device with a fingerprint sensor. You must implement the user interface for the fingerprint authentication flow on your app, and use the standard Android fingerprint icon in your UI. The Android fingerprint icon (c_fp_40px.png) is included in the sample app. If you are developing multiple apps that use fingerprint authentication, note that each app must authenticate the user’s fingerprint independently.

为了通过指纹扫描进行用户身份验证,需获取新类FingerprintManager的一个实例并调用方法authenticate()。你的应用程序必须运行在一个兼容指纹感应器的设备上。在你的App上你必须为指纹认证流程实现一个用户界面,并且在应用UI中使用标准的Android指纹认证图标。Android指纹认证图标(c_fp_40px.png)包含在示例程序中。如果你正开发多个使用指纹认证的应用程序,需要注意每一个应用都必须独立地验证用户的指纹。

To use this feature in your app, first add the USE_FINGERPRINT permission in your manifest.

为了在你的App中使用该特性,首先需要在你的清单配置文件manifest中添加USE_FINGERPRINT权限:

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

To see an app implementation of fingerprint authentication, refer to the Fingerprint Dialog sample. For a demonstration of how you can use these authentication APIs in conjunction with other Android APIs, see the video Fingerprint and Payment APIs.

如要查看一个指纹认证的应用程序实现,请查阅Fingerprint Dialog sample.视频Fingerprint and Payment APIs,示范了如何结合使用这些身份验证APIs以及其他的Android APIs。

If you are testing this feature, follow these steps:

如果你正在测试该特性,那么跟随如下步骤:

  1. Install Android SDK Tools Revision 24.3, if you have not done so.

先安装Android SDK Tools Revision 24.3,如果你还没有完成这一步的话。

  1. Enroll a new fingerprint in the emulator by going to Settings > Security > Fingerprint, then follow the enrollment instructions.

在模拟器中依次点击设置>安全>指纹以添加一个新的指纹,之后按照指纹登记指南来做即可。

  1. Use an emulator to emulate fingerprint touch events with the following command. Use the same command to emulate fingerprint touch events on the lockscreen or in your app.

使用一个模拟器及如下命令来模拟指纹触摸事件。使用同样的命令在屏幕上面或者在你的应用程序中模拟指纹触摸事件。

adb -e emu finger touch <finger_id>

On Windows, you may have to run telnet 127.0.0.1

在Windows系统上,你可能需要运行如下命令:telnet 127.0.0.1

<emulator-id> followed by finger touch <finger_id>.

<emulator-id>紧跟finger touch <finger_id>之后。

Confirm Credential 确认凭证

Your app can authenticate users based on how recently they last unlocked their device. This feature frees users from having to remember additional app-specific passwords, and avoids the need for you to implement your own authentication user interface. Your app should use this feature in conjunction with a public or secret key implementation for user authentication.

你的应用程序可以基于用户最近一次解锁其设备的方式来进行用户身份验证。该特性使用户从不得不记住额外的特定于应用程序的密码中解脱出来,同时也避免了你来实现自己的用户身份验证界面的必要。对于用户身份验证而言,你的应用程序应该将该特性同一个公开的或者私密的密钥实现一块协同使用。

To set the timeout duration for which the same key can be re-used after a user is successfully authenticated, call the new setUserAuthenticationValidityDurationSeconds() method when you set up a KeyGenerator or KeyPairGenerator.

为了设置相同的Key在某个用户成功进行身份验证之后的超时时间,当你搭建一个KeyGenerator或者KeyPairGenerator时调用新方法setUserAuthenticationValidityDurationSeconds()

Avoid showing the re-authentication dialog excessively -- your apps should try using the cryptographic object first and if the the timeout expires, use the createConfirmDeviceCredentialIntent() method to re-authenticate the user within your app.

为了避免重新验证身份对话框的过度显示,首先你的应用程序应当尽可能的使用加密对象,如果超时失效了,使用createConfirmDeviceCredentialIntent()方法以在你的应用程序中重新验证用户身份。

To see an app implementation of this feature, refer to the Confirm Credential sample

查看该特性的应用程序实现,请查阅Confirm Credential sample

Direct Share 直接分享

This preview provides you with APIs to make sharing intuitive and quick for users. You can now define direct share targets that launch a specific activity in your app. These direct share targets are exposed to users via the Share menu. This feature allows users to share content to targets, such as contacts, within other apps. For example, the direct share target might launch an activity in another social network app, which lets the user share content directly to a specific friend or community in that app.

该预览会向你提供一些让分享对用户而言更直观快速的APIs。现在你可以定义直接分享组件,以在你的App中启动某个特定的activity。这些直接分享组件会通过分享菜单暴露给用户。该特性允许用户在其它应用程序中分享特定内容到targets,诸如联系人等。例如,直接分享组件可以启动其它社交网络类App中的某个activity,这将让用户能够在这些应用程序中直接分享特定内容到某个特定的朋友或者社区中。

To enable direct share targets you must define a class that extends the ChooserTargetService class. Declare your service in the manifest. Within that declaration, specify the BIND_CHOOSER_TARGET_SERVICE permission and an intent filter using the SERVICE_INTERFACE action.

为了启用直接分享组件,你必须定义一个继承自ChooserTargetService的类。在清单文件manifest中定义你的服务。在该定义中,指定BIND_CHOOSER_TARGET_SERVICE权限和一个使用BIND_CHOOSER_TARGET_SERVICE动作的意图过滤器。

The following example shows how you might declare the ChooserTargetService in your manifest.

如下示例展示了如何在清单文件中定义ChooserTargetService.

<service android:name=".ChooserTargetService"
android:label="@string/service_name"
android:permission="android.permission.BIND_CHOOSER_TARGET_SERVICE">
 <intent-filter>
  <action
android:name="android.service.chooser.ChooserTargetService"/>
 </intent-filter>
</service>

For each activity that you want to expose to ChooserTargetService, add a <meta-data> element with the name "android.service.chooser.chooser_target_service" in your app manifest.

对于每个你想要暴露给ChooserTargetService的activity,在你的清单文件中添加一个使用name为"android.service.chooser.chooser_target_service"的<meta-data>标签。

<activity android:name=".MyShareActivity”
        android:label="@string/share_activity_label">
 <intent-filter>
        <action android:name="android.intent.action.SEND" />
 </intent-filter>
  <meta-data
     android:name="android.service.chooser.chooser_target_service"
     android:value=".ChooserTargetService" />
</activity>

Voice Interactions 语音交互

This preview provides a new voice interaction API which, together with Voice Actions, allows you to build conversational voice experiences into your apps. Call the isVoiceInteraction() method to determine if a voice action triggered your activity. If so, your app can use the VoiceInteractor class to request a voice confirmation from the user, select from a list of options, and more.

该预览提供了一个新的语音交互API,与Voice Actions相同,允许你在应用程序中构建对话式语音体验。调用方法isVoiceInteraction()以确定你的activity是否触发了一个语音活动。如果是,你的应用程序可以使用类VoiceInteractor向用户请求语音确认,选择一系列选项,等等。

Most voice interactions originate from a user voice action. A voice interaction activity can also, however, start without user input. For example, another app launched through a voice interaction can also send an intent to launch a voice interaction. To determine if your activity launched from a user voice query or from another voice interaction app, call the isVoiceInteractionRoot() method. If another app launched your activity, the method returns false. Your app may then prompt the user to confirm that they intended this action.

多数的语音交互来源于一个用户的语音活动。一个语音交互activity也可以这样,然而,无需用户输入。比如,另一个通过某个语音交互启动的应用程序也可以发送一个意图来启动一个语音交互。调用方法isVoiceInteractionRoot()以确定你的activity是否被某个用户语音查询或者其他语音交互App启动了。如果另一个App启动了你的activity,该方法会返回false。你的App之后可以提示用户以确认他们对该活动的打算。

To learn more about implementing voice actions, see the Voice Actions developer site.

查看章节Voice Actions developer site以更多的了解语音活动实现。

Assist API 辅助的API

This preview offers a new way for users to engage with your apps through an assistant. To use this feature, the user must enable the assistant to use the current context. Once enabled, the user can summon the assistant within any app, by long-pressing on the Home button.

该预览为用户提供了一种,通过一个助手同你的App交互,的新方式。为了使用该特性,用户必须启用使用当前上下文起用该助手。一旦启用,用户可以在任何App中唤起该助手,通过长按住Home按钮即可。

To provide the assistant with additional context from your app, follow these steps:

按照如下步骤,即可使用应用程序的附加上下文来添加该助手。

  1. Implement the Application.OnProvideAssistDataListener interface.

实现接口Application.OnProvideAssistDataListener

  1. Register this listener by using registerOnProvideAssistDataListener().

使用registerOnProvideAssistDataListener()方法注册该监听器。

  1. In order to provide activity-specific contextual information, override the onProvideAssistData() callback and, optionally, the new onProvideAssistContent() callback.

为了能够提供特定于activity的上下文关联的信息,重写回调函数onProvideAssistData()并有选择性地重写新的回调函数onProvideAssistContent()

 

Notifications 通知

This preview adds the following API changes for notifications:

该预览对通知添加了如下的API更改:

  • New INTERRUPTION_FILTER_ALARMS filter level that corresponds to the new Alarms only do not disturb mode.

新的INTERRUPTION_FILTER_ALARMS过滤器级别以符合新的Alarms only请勿打扰模式。

  • New CATEGORY_REMINDER category value that is used to distinguish user-scheduled reminders from other events (CATEGORY_EVENT) and alarms (CATEGORY_ALARM).

新的CATEGORY_REMINDER类型值,用于区分用户定时提醒与其他的事件(CATEGORY_EVENT)及闹钟(CATEGORY_EVENT)。

  • New Icon class that you can attach to your notifications via the setSmallIcon() and setLargeIcon() methods. Similarly, the addAction() method now accepts an Icon object instead of a drawable resource ID.

新的Icon类,可以通过方法setSmallIcon()和方法setSmallIcon()将该类与通知相关联。

  • New getActiveNotifications() method that allows your apps to find out which of their notifications are currently alive. To see an app implementation that uses this feature, see the Active Notifications sample

新的方法getActiveNotifications(),允许你的应用程序找出在所有通知中当前活动的通知。查阅Active Notifications sample以查看该特性使用的应用程序实现。

Bluetooth Stylus Support  蓝牙手写笔支持

This preview provides improved support for user input using a Bluetooth stylus. Users can pair and connect a compatible Bluetooth stylus with their phone or tablet. While connected, position information from the touch screen is fused with pressure and button information from the stylus to provide a greater range of expression than with the touch screen alone. Your app can listen for stylus button presses and perform secondary actions, by registering View.OnContextClickListener and GestureDetector.OnContextClickListener objects in your activity.

该预览对使用了一个蓝牙手写笔的用户输入提供了一个改进的支持。用户可以匹配并连接一个兼容的蓝牙手写笔和他们的手机或者平板。当连接之后,通过屏幕触摸产生的位置信息会转为压力,同时通过手写笔产生的按钮信息相比仅仅触摸屏幕而言会提供一个更大的表述范围。通过在activity中注册View.OnContextClickListenerView.OnContextClickListener对象,你的应用程序可以监听手写笔的按钮及压力,执行次要动作。

Use the MotionEvent methods and constants to detect stylus button interactions:

使用MotionEvent类中的方法和变量来检测手写笔按钮活动:

If the user touches a stylus with a button on the screen of your app, the getTooltype() method returns TOOL_TYPE_STYLUS.

如果用户通过应用程序屏幕上的一个按钮触摸了一个手写笔,方法getTooltype()会返回getTooltype()

For apps targeting M Preview, the getButtonState() method returns BUTTON_STYLUS_PRIMARY when the user presses the primary stylus button. If the stylus has a second button, the same method returns BUTTON_STYLUS_SECONDARY when the user presses it. If the user presses both buttons simultaneously, the method returns both values OR'ed together (BUTTON_STYLUS_PRIMARY|BUTTON_STYLUS_SECONDARY).

对于以M Preview为目标系统的应用程序而言,当用户按下主要的手写笔按钮时方法getButtonState()会返回BUTTON_STYLUS_PRIMARY。如果手写笔有第二个按钮,当用户按下该按钮时该方法会返回BUTTON_STYLUS_SECONDARY。如果用户同事按下了两个按钮的话,该方法会同时返回这两个值相互或运算之后的值(BUTTON_STYLUS_PRIMARY|BUTTON_STYLUS_SECONDARY)。

上一篇:后台组装报文,返回app前端


下一篇:枚举类型转为Map使用