你的位置:首页 > 操作系统

[操作系统]Android M Developer 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.



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.



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


<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


<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.


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.


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.


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.


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


<service android:name=".ChooserTargetService"

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.


<activity android:name=".MyShareActivity”
        <action android:name="android.intent.action.SEND" />
     android:value=".ChooserTargetService" />

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.


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.



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


  1. Implement the Application.OnProvideAssistDataListener interface.


  1. Register this listener by using registerOnProvideAssistDataListener().


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



Notifications 通知

This preview adds the following API changes for notifications:


  • 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).


  • 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.


  • 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.


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


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


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)。