注:本文是原創(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實現(xiàn)的變化。這篇文章系宜,主要是介紹那些,在你開發(fā)app時,有必要明白和理解的關(guān)鍵性變化冒滩。如果你之前有開發(fā)過Android的經(jīng)驗轿衔,那你得關(guān)心一下害驹,這些修改將會對你的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 引進了新的權(quán)限模式『ヒ荆現(xiàn)在,通過這個模式扛稽,可以使得用戶在app運行的過程中,直接的管理app的權(quán)限,它也提供給用戶更為直觀的管理權(quán)限功能,以及提供一體化的安裝和自動更新程序給開發(fā)者。對于用戶來說,他還可以在安裝app的時候,直接地賦予或拒絕權(quán)限。
如果你的app的target值在23或者更高,那請確保你是在app的運行過程中,才檢查和請求權(quán)限的美澳。在此基礎(chǔ)上酱虎,如果你想判斷app是否擁有某個權(quán)限读串,可以調(diào)用*[checkSelfPermission()](https://developer.android.com/reference/android/content/Context.html#checkSelfPermission%28java.lang.String%29)*方法聊记。如果你想為app申請某個權(quán)限撒妈,則可以通過調(diào)用 *[requestPermissions()](https://developer.android.com/reference/android/app/Activity.html#requestPermissions%28java.lang.String%5B%5D,%20int%29)* 方法來實現(xiàn)。即使你的App targeting 并不是23排监,你也應該測試一下你的app在新的權(quán)限模式下的運行效果狰右。
關(guān)于如何在你的app上,更好的支持新的權(quán)限管理模式的詳細介紹舆床,請看 [Working with System Permission](https://developer.android.com/training/permissions/index.html)一文棋蚌。對于如何使你的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 也引進了新的省電模式(一共兩種,一種針對設(shè)備盛垦,一種針對設(shè)備上的app)湿弦,主要是用來針對那些處于“空閑狀態(tài)”的設(shè)備(手機、平板腾夯、車載)和app颊埃。這個省電模式,將會影響所有的app俯在。所以竟秫,請確保在這個省電模式下,有測試過你的app跷乐。
Doze: 如果用戶的設(shè)備處于非充電狀態(tài)肥败,并且它的屏幕已經(jīng)關(guān)閉了一段時間。那么愕提,設(shè)備將會自動進入Doze模式馒稍,它會試圖讓系統(tǒng)處于一種休眠狀態(tài)。在這種模式下浅侨,設(shè)備會不定期地纽谒,在短時間內(nèi)重啟一些普通的操作,來使app可以同步數(shù)據(jù)如输,以及讓Android系統(tǒng)執(zhí)行一些應該做的事情鼓黔。
App Standby: 這個模式,允許系統(tǒng)不见,在用戶沒有再繼續(xù)操作app的情況下澳化,決定哪一個app是處于空閑狀態(tài)。系統(tǒng)會通過監(jiān)聽用戶在一段時間內(nèi)稳吮,是否沒有再操作app缎谷,來判定某一個app是否是處于空閑狀態(tài)。當設(shè)備處于非充電狀態(tài)灶似,系統(tǒng)將會斷開網(wǎng)絡(luò)連接列林,并中斷所有app的數(shù)據(jù)同步和工作(相當于認為設(shè)備是空閑的)瑞你。
為習更多的有關(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 取消了對 Apache HTTP client的支持希痴。如果你的app者甲,正在使用這個類的API進行網(wǎng)絡(luò)請求,并且你的app的targets在9及以上砌创。那么过牙,你應該使用[HttpURLConnection](https://developer.android.com/reference/java/net/HttpURLConnection.html) 類來代替。因為纺铭,這個類的API可以更加有效率的進行網(wǎng)絡(luò)請求寇钉。而這主要體現(xiàn)在,它可以減少網(wǎng)絡(luò)傳輸包的大小和響應緩存舶赔,并且可以使網(wǎng)絡(luò)請求所耗費的電量扫倡,降到最小。如果竟纳,你還是想要在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ā),那請不要再使用cryptographic的庫了。因為栅炒,這些庫并不屬于NDK的API的一部分,比如libcrypto.so 和libssl.so惶翻。這些庫,也不屬于公共的通用API鹅心。所以吕粗,有可能會在版本迭代的過程中,被修改和刪除掉旭愧,而這并不會有任何通知告訴你颅筋,它們已經(jīng)被修改或刪除了。除此之外输枯,這也會導致你自己將自己的安全缺陷暴露出來(沒搞過NDK開發(fā)议泵,不知道暴的啥安全缺陷)。所以用押,為了避免上述情況的發(fā)生肢簿,取而代之的做法是靶剑,你應該修改本地的代碼蜻拨,通過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ù)保護血崭,從Android 6.0開始卧惜,app在使用WIFI和藍牙API的時候,Android將不會自動授權(quán)訪問設(shè)備的本地硬件標識(MAC地址)夹纫。如果你想調(diào)用WifiInfo.getMacAddress() 和 BluetoothAdapter.getAddress()方法來獲取MAC地址咽瓷,那現(xiàn)在,得到的將是一個常量值: 02:00:00:00:00:00.
如果想通過舰讹,藍牙和WIFI的掃描茅姜,來獲取附近的外部設(shè)備的硬件標識。你必須聲明下面的兩個權(quán)限之一: ACCESS_FINE_LOCATION 或 ACCESS_COARSE_LOCATION permissions:
WifiManager.getScanResults()
BluetoothDevice.ACTION_FOUND
BluetoothLeScanner.startScan()
注: 當一個運行在6.0系統(tǒng)的機子月匣,在后臺進行WIFI或藍牙掃描時钻洒,你的這個操作,將會在其它手機上锄开,以一個隨機的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+的機子,都能夠?qū)崿F(xiàn)安裝時權(quán)限授予和運行過程中授予的功能了(也有可能是產(chǎn)商自已定制癣诱,非安卓自帶)任岸。
2、關(guān)于省電管理狡刘。App Standby 段里享潜,關(guān)于設(shè)備處于非充電狀態(tài),就會自動斷網(wǎng)和中斷所有app的數(shù)據(jù)同步和工作嗅蔬。這有點夸張剑按,不知道是不是我翻譯的有問題。如果處于鎖屏狀態(tài)澜术,那還正常艺蝴,也有可能是原文寫少了。
先翻譯這部分鸟废,剩下的明天猜敢,再翻譯。