Android 6.0 新特性

注:本文是原創(chuàng)翻譯Android 6.0 Changes一文,附上原文地址:

https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-runtime-permissions

**Introduction**

Along with new features and capabilities, Android 6.0 (API level 23) includes a variety of system changes and API behavior changes. This document highlights some of the key changes that you should understand and account for in your apps.? If you have previously published an app for Android, be aware that these changes in the platform affect your app.?

伴隨著新的特性和功能的產(chǎn)生,Android 6.0 發(fā)生了一系列的系統(tǒng)和API實(shí)現(xiàn)的變化。這篇文章,主要是介紹那些,在你開發(fā)app時(shí),有必要明白和理解的關(guān)鍵性變化。如果你之前有開發(fā)過Android的經(jīng)驗(yàn),那你得關(guān)心一下,這些修改將會(huì)對(duì)你的app所產(chǎn)生的影響。

**Runtime Permissions**

This release introduces a new permissions model, where users can now directly manage app permissions at runtime. This model gives users improved visibility and control over permissions, while streamlining the installation and auto-update processes for app developers. Users can grant or revoke permissions individually for installed apps.

On your apps that target Android 6.0 (API level 23) or higher, make sure to check for and request permissions at runtime. To determine if your app has been granted a permission, call the new checkSelfPermission() method. To request a permission, call the new requestPermissions() method. Even if your app is not targeting Android 6.0 (API level 23), you should test your app under the new permissions model.

For details on supporting the new permissions model in your app, see Working with System Permissions. For tips on how to assess the impact on your app, see Permissions Best Practices.

Android 6.0 引進(jìn)了新的權(quán)限模式。現(xiàn)在,通過這個(gè)模式,可以使得用戶在app運(yùn)行的過程中,直接的管理app的權(quán)限,它也提供給用戶更為直觀的管理權(quán)限功能,以及提供一體化的安裝和自動(dòng)更新程序給開發(fā)者。對(duì)于用戶來說,他還可以在安裝app的時(shí)候,直接地賦予或拒絕權(quán)限。

如果你的app的target值在23或者更高,那請(qǐng)確保你是在app的運(yùn)行過程中,才檢查和請(qǐng)求權(quán)限的。在此基礎(chǔ)上,如果你想判斷app是否擁有某個(gè)權(quán)限,可以調(diào)用*[checkSelfPermission()](https://developer.android.com/reference/android/content/Context.html#checkSelfPermission%28java.lang.String%29)*方法。如果你想為app申請(qǐng)某個(gè)權(quán)限,則可以通過調(diào)用 *[requestPermissions()](https://developer.android.com/reference/android/app/Activity.html#requestPermissions%28java.lang.String%5B%5D,%20int%29)* 方法來實(shí)現(xiàn)。即使你的App targeting 并不是23,你也應(yīng)該測(cè)試一下你的app在新的權(quán)限模式下的運(yùn)行效果。

關(guān)于如何在你的app上,更好的支持新的權(quán)限管理模式的詳細(xì)介紹,請(qǐng)看 [Working with System Permission](https://developer.android.com/training/permissions/index.html)一文。對(duì)于如何使你的app更好的兼容新模式,建議你去看看[Permissions Best Practices](https://developer.android.com/training/permissions/best-practices.html#testing)。

**Doze and App Standby**

This release introduces new power-saving optimizations for idle devices and apps. These features affect all apps so make sure to test your apps in these new modes.

Doze: If a user unplugs a device and leaves it stationary, with its screen off, for a period of time, the device goes into Doze mode, where it attempts to keep the system in a sleep state. In this mode, devices periodically resume normal operations for brief periods of time so that app syncing can occur and the system can perform any pending operations.

App Standby: App Standby allows the system to determine that an app is idle when the user is not actively using it. The system makes this determination when the user does not touch the app for a certain period of time. If the device is unplugged, the system disables network access and suspends syncs and jobs for the apps it deems idle.

To learn more about these power-saving changes, see Optimizing for Doze and App Standby.

同樣地,Android 6.0 也引進(jìn)了新的省電模式(一共兩種,一種針對(duì)設(shè)備,一種針對(duì)設(shè)備上的app),主要是用來針對(duì)那些處于“空閑狀態(tài)”的設(shè)備(手機(jī)、平板、車載)和app。這個(gè)省電模式,將會(huì)影響所有的app。所以,請(qǐng)確保在這個(gè)省電模式下,有測(cè)試過你的app。

Doze: 如果用戶的設(shè)備處于非充電狀態(tài),并且它的屏幕已經(jīng)關(guān)閉了一段時(shí)間。那么,設(shè)備將會(huì)自動(dòng)進(jìn)入Doze模式,它會(huì)試圖讓系統(tǒng)處于一種休眠狀態(tài)。在這種模式下,設(shè)備會(huì)不定期地,在短時(shí)間內(nèi)重啟一些普通的操作,來使app可以同步數(shù)據(jù),以及讓Android系統(tǒng)執(zhí)行一些應(yīng)該做的事情。

App Standby: 這個(gè)模式,允許系統(tǒng),在用戶沒有再繼續(xù)操作app的情況下,決定哪一個(gè)app是處于空閑狀態(tài)。系統(tǒng)會(huì)通過監(jiān)聽用戶在一段時(shí)間內(nèi),是否沒有再操作app,來判定某一個(gè)app是否是處于空閑狀態(tài)。當(dāng)設(shè)備處于非充電狀態(tài),系統(tǒng)將會(huì)斷開網(wǎng)絡(luò)連接,并中斷所有app的數(shù)據(jù)同步和工作(相當(dāng)于認(rèn)為設(shè)備是空閑的)。

為習(xí)更多的有關(guān)省電模式的修改,可以看[Optimizing for Doze and App Standby](https://developer.android.com/training/monitoring-device-state/doze-standby.html).

**Apache HTTP Client Removal**

Android 6.0 release removes support for the Apache HTTP client. If your app is using this client and targets Android 2.3 (API level 9) or higher, use the HttpURLConnection class instead. This API is more efficient because it reduces network use through transparent compression and response caching, and minimizes power consumption. To continue using the Apache HTTP APIs, you must first declare the following compile-time dependency in your build.gradle file:

```

android {

? ? useLibrary 'org.apache.http.legacy'

}

```

Android 6.0 取消了對(duì) Apache HTTP client的支持。如果你的app,正在使用這個(gè)類的API進(jìn)行網(wǎng)絡(luò)請(qǐng)求,并且你的app的targets在9及以上。那么,你應(yīng)該使用[HttpURLConnection](https://developer.android.com/reference/java/net/HttpURLConnection.html) 類來代替。因?yàn)?,這個(gè)類的API可以更加有效率的進(jìn)行網(wǎng)絡(luò)請(qǐng)求。而這主要體現(xiàn)在,它可以減少網(wǎng)絡(luò)傳輸包的大小和響應(yīng)緩存,并且可以使網(wǎng)絡(luò)請(qǐng)求所耗費(fèi)的電量,降到最小。如果,你還是想要在6.0上,繼續(xù)使用Apache HTTP的api。那你必須在你的build.gradle文件里,添加如下代碼:

```

android {

? ? useLibrary 'org.apache.http.legacy'

}

```

**BoringSSL**

Android is moving away from OpenSSL to the BoringSSL library. If you’re using the Android NDK in your app, don't link against cryptographic libraries that are not a part of the NDK API, such as libcrypto.so and libssl.so. These libraries are not public APIs, and may change or break without notice across releases and devices. In addition, you may expose yourself to security vulnerabilities. Instead, modify your native code to call the Java cryptography APIs via JNI or to statically link against a cryptography library of your choice.

Android 系統(tǒng)正在逐步使用BoringSSL庫來代替OpenSSL庫的使用。如果你的app,涉及到NDK的開發(fā),那請(qǐng)不要再使用cryptographic的庫了。因?yàn)?,這些庫并不屬于NDK的API的一部分,比如libcrypto.so 和libssl.so。這些庫,也不屬于公共的通用API。所以,有可能會(huì)在版本迭代的過程中,被修改和刪除掉,而這并不會(huì)有任何通知告訴你,它們已經(jīng)被修改或刪除了。除此之外,這也會(huì)導(dǎo)致你自己將自己的安全缺陷暴露出來(沒搞過NDK開發(fā),不知道暴的啥安全缺陷)。所以,為了避免上述情況的發(fā)生,取而代之的做法是,你應(yīng)該修改本地的代碼,通過JNI來調(diào)用Java的cryptography庫的api?;蛘撸ㄟ^靜態(tài)鏈接,來調(diào)用你所需要的cryptography庫。

**Access to Hardware Identifier**

To provide users with greater data protection, starting in this release, Android removes programmatic access to the device’s local hardware identifier for apps using the Wi-Fi and Bluetooth APIs. The WifiInfo.getMacAddress() and the BluetoothAdapter.getAddress() methods now return a constant value of 02:00:00:00:00:00.

To access the hardware identifiers of nearby external devices via Bluetooth and Wi-Fi scans, your app must now have the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions:

WifiManager.getScanResults()

BluetoothDevice.ACTION_FOUND

BluetoothLeScanner.startScan()

Note: When a device running Android 6.0 (API level 23) initiates a background Wi-Fi or Bluetooth scan, the operation is visible to external devices as originating from a randomized MAC address.

為了給用戶提供更好的數(shù)據(jù)保護(hù),從Android 6.0開始,app在使用WIFI和藍(lán)牙API的時(shí)候,Android將不會(huì)自動(dòng)授權(quán)訪問設(shè)備的本地硬件標(biāo)識(shí)(MAC地址)。如果你想調(diào)用WifiInfo.getMacAddress() 和 BluetoothAdapter.getAddress()方法來獲取MAC地址,那現(xiàn)在,得到的將是一個(gè)常量值: 02:00:00:00:00:00.

如果想通過,藍(lán)牙和WIFI的掃描,來獲取附近的外部設(shè)備的硬件標(biāo)識(shí)。你必須聲明下面的兩個(gè)權(quán)限之一: ACCESS_FINE_LOCATION 或 ACCESS_COARSE_LOCATION permissions:

WifiManager.getScanResults()

BluetoothDevice.ACTION_FOUND

BluetoothLeScanner.startScan()

注: 當(dāng)一個(gè)運(yùn)行在6.0系統(tǒng)的機(jī)子,在后臺(tái)進(jìn)行WIFI或藍(lán)牙掃描時(shí),你的這個(gè)操作,將會(huì)在其它手機(jī)上,以一個(gè)隨機(jī)的MAC地址的形式,展現(xiàn)出來。

**Notifications**

This release removes the Notification.setLatestEventInfo() method. Use the Notification.Builder class instead to construct notifications. To update a notification repeatedly, reuse the Notification.Builder instance. Call the build() method to get updated Notification instances.

The adb shell dumpsys notification command no longer prints out your notification text. Use the adb shell dumpsys notification --noredact command instead to print out the text in a notification object.

AudioManager Changes

注:

1、關(guān)于權(quán)限管理,現(xiàn)在大部分的4.0+和5.0+的機(jī)子,都能夠?qū)崿F(xiàn)安裝時(shí)權(quán)限授予和運(yùn)行過程中授予的功能了(也有可能是產(chǎn)商自已定制,非安卓自帶)。

2、關(guān)于省電管理。App Standby 段里,關(guān)于設(shè)備處于非充電狀態(tài),就會(huì)自動(dòng)斷網(wǎng)和中斷所有app的數(shù)據(jù)同步和工作。這有點(diǎn)夸張,不知道是不是我翻譯的有問題。如果處于鎖屏狀態(tài),那還正常,也有可能是原文寫少了。

先翻譯這部分,剩下的明天,再翻譯。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容