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實現(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)澜术,那還正常艺蝴,也有可能是原文寫少了。

先翻譯這部分鸟废,剩下的明天猜敢,再翻譯。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市缩擂,隨后出現(xiàn)的幾起案子鼠冕,更是在濱河造成了極大的恐慌,老刑警劉巖胯盯,帶你破解...
    沈念sama閱讀 216,402評論 6 499
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件懈费,死亡現(xiàn)場離奇詭異,居然都是意外死亡博脑,警方通過查閱死者的電腦和手機憎乙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,377評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來叉趣,“玉大人泞边,你說我怎么就攤上這事×粕迹” “怎么了繁堡?”我有些...
    開封第一講書人閱讀 162,483評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長乡数。 經(jīng)常有香客問我椭蹄,道長,這世上最難降的妖魔是什么净赴? 我笑而不...
    開封第一講書人閱讀 58,165評論 1 292
  • 正文 為了忘掉前任绳矩,我火速辦了婚禮,結(jié)果婚禮上玖翅,老公的妹妹穿的比我還像新娘翼馆。我一直安慰自己,他們只是感情好金度,可當我...
    茶點故事閱讀 67,176評論 6 388
  • 文/花漫 我一把揭開白布应媚。 她就那樣靜靜地躺著,像睡著了一般猜极。 火紅的嫁衣襯著肌膚如雪中姜。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,146評論 1 297
  • 那天跟伏,我揣著相機與錄音丢胚,去河邊找鬼。 笑死受扳,一個胖子當著我的面吹牛携龟,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播勘高,決...
    沈念sama閱讀 40,032評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼峡蟋,長吁一口氣:“原來是場噩夢啊……” “哼坟桅!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起蕊蝗,我...
    開封第一講書人閱讀 38,896評論 0 274
  • 序言:老撾萬榮一對情侶失蹤仅乓,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后匿又,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,311評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡建蹄,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,536評論 2 332
  • 正文 我和宋清朗相戀三年碌更,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片洞慎。...
    茶點故事閱讀 39,696評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡痛单,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出劲腿,到底是詐尸還是另有隱情旭绒,我是刑警寧澤,帶...
    沈念sama閱讀 35,413評論 5 343
  • 正文 年R本政府宣布焦人,位于F島的核電站挥吵,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏花椭。R本人自食惡果不足惜忽匈,卻給世界環(huán)境...
    茶點故事閱讀 41,008評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望矿辽。 院中可真熱鬧丹允,春花似錦、人聲如沸袋倔。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽宾娜。三九已至批狐,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間前塔,已是汗流浹背贾陷。 一陣腳步聲響...
    開封第一講書人閱讀 32,815評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留嘱根,地道東北人髓废。 一個月前我還...
    沈念sama閱讀 47,698評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像慌洪,于是被迫代替她去往敵國和親顶燕。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,592評論 2 353

推薦閱讀更多精彩內(nèi)容