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
即將到來的Android平臺的釋放版本將會爲用戶以及App開發者提供新的功能特性,M Developer Preview則爲此提供一個預覽。本文檔將會向你們介紹大多數值得注意的APIs。git
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!github
M Developer Preview面向前期的開發人員和測試人員。若是你有意直接做用於Android frameword,不妨嘗試下M Developer Preview,並向咱們發送你的反饋!web
Caution: Do not publish apps that use the M Developer Preview to the Google Play store.express
警告:不要推送那些使用了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中能夠下載。app
If you have previously published an app for Android, be aware that your app might be affected by changes in the platform.dom
若是你以前已經推送了一款Android平臺下的App,你會發覺你的App可能會受到新平臺中行爲的更改而帶來的影響。ide
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,以學習如何實現該特性。
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。
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吧。
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:
若是你正在測試該特性,那麼跟隨以下步驟:
先安裝Android SDK Tools Revision 24.3,若是你尚未完成這一步的話。
在模擬器中依次點擊設置>安全>指紋以添加一個新的指紋,以後按照指紋登記指南來作便可。
使用一個模擬器及以下命令來模擬指紋觸摸事件。使用一樣的命令在屏幕上面或者在你的應用程序中模擬指紋觸摸事件。
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>以後。
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。
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:name="android.service.chooser.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.
對於每一個你想要暴露給ChooserTargetService的activity,在你的清單文件中添加一個使用name爲"android.service.chooser.chooser_target_service"的<meta-data>標籤。
<activity android:name=".MyShareActivity」 |
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以更多的瞭解語音活動實現。
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:
按照以下步驟,便可使用應用程序的附加上下文來添加該助手。
實現接口Application.OnProvideAssistDataListener。
使用registerOnProvideAssistDataListener()方法註冊該監聽器。
爲了可以提供特定於activity的上下文關聯的信息,重寫回調函數onProvideAssistData()並有選擇性地重寫新的回調函數
onProvideAssistContent()。
This preview adds the following API changes for notifications:
該預覽對通知添加了以下的API更改:
新的INTERRUPTION_FILTER_ALARMS過濾器級別以符合新的Alarms only請勿打擾模式。
新的CATEGORY_REMINDER類型值,用於區分用戶定時提醒與其餘的事件
(CATEGORY_EVENT)及鬧鐘(CATEGORY_EVENT)。
新的Icon類,能夠經過方法setSmallIcon()和方法
setSmallIcon()將該類與通知相關聯。
新的方法getActiveNotifications(),容許你的應用程序找出在全部通知中當前活動的通知。查閱
Active Notifications sample以查看該特性使用的應用程序實現。
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.OnContextClickListener和
View.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)。