1 page.title=Cc thay i Hnh vi 2 page.keywords=preview,sdk,compatibility 3 meta.tags="preview", "compatibility" 4 page.tags="preview", "developer preview" 5 page.image=images/cards/card-n-changes_2x.png 6 @jd:body 7 8 9 <div id="tb-wrapper"> 10 <div id="tb"> 11 12 <h2>Trong ti liu ny</h2> 13 14 <ol> 15 <li><a href="#perf">Ci thin Hiu nng</a> 16 <ol> 17 <li><a href="#doze">Ch Ng su</a></li> 18 <li><a href="#bg-opt">Ti u ha Chy ngm</a></li> 19 </ol> 20 </li> 21 <li><a href="#perm">Thay i Quyn</a></li> 22 <li><a href="#accessibility">Ci thin Tr nng</a> 23 <ol> 24 <li><a href="#screen-zoom">Thu phng Mn hnh</a></li> 25 <li><a href="#vision-settings">Ci t Kh nng nhn trong Trnh Thit lp</a></li> 26 </ol> 27 </li> 28 <li><a href="#ndk">Lin kt ng dng NDK vi Th vin Nn tng</a></li> 29 <li><a href="#afw">Android cho Cng vic</a></li> 30 </ol> 31 32 <h2>Xem thm</h2> 33 <ol> 34 <li><a href="{@docRoot}preview/api-overview.html"> 35 Tng quan v API Android N</a></li> 36 </ol> 37 38 </div> 39 </div> 40 41 42 <p> 43 Vi cc tnh nng v kh nng mi, Android N 44 cng c mt s cc thay i hnh vi ca h thng v API. Ti liu 45 ny nu bt mt s cc thay i quan trng m bn cn bit v cn nhc cho 46 ng dng ca mnh. 47 </p> 48 49 <p> 50 Nu bn tng pht hnh ng dng cho Android, hy lu rng cc thay i 51 trong nn tng ny c th tc ng n ng dng ca bn. 52 </p> 53 54 55 <h2 id="perf">Ci thin Hiu nng</h2> 56 57 <p> 58 Android N c cha cc thay i hnh vi ca h thng nhm ci thin thi lng 59 pin ca thit b, s dng RAM v hiu nng ng dng. Cc thay i ny c th tc ng n 60 tnh kh dng ca ti nguyn v cc thng bo h thng i ti ng dng. Bn 61 nn tm hiu cc thay i ny v nh gi xem ng dng cn phi iu chnh nh th no i vi 62 cc thay i. 63 </p> 64 65 <h3 id="doze">Ch Ng su</h3> 66 67 <p> 68 c a vo Android 6.0 (Mc API 23), Ch Ng su ci thin thi lng pin bng cch 69 tr hon cc hot ng ca CPU v mng khi ngi dng khng cm sc, 70 khng di chuyn v tt mn hnh thit b. Android N 71 thm cc ci tin cho Ch Ng su bng cch s dng mt tp con cc hn ch ca CPU v mng 72 khi thit b khng c cm sc vi mn hnh b tt, nhng khng nht thit 73 phi mt ch, v d nh mt thit b cm tay di chuyn trong ti ca ngi dng. 74 </p> 75 76 77 <img src="{@docRoot}images/android-7.0/doze-diagram-1.png" alt="" height="251px" id="figure1" /> 78 <p class="img-caption"> 79 <strong>Hnh 1.</strong> Minh ha cch thc Ch Ng su p dng cp u tin ca 80 cc hn ch hot ng h thng nhm ci thin thi lng pin. 81 </p> 82 83 <p> 84 Khi mt thit b dng ngun in t pin v mn hnh b tt trong mt khong thi gian 85 nht nh, thit b s chuyn vo Ch Ng su v p dng tp con u tin ca cc hn ch: N 86 s ngt truy cp mng ca ng dng v tr hon cc tc v v cc ng b. Nu thit b 87 c t mt ch trong mt khong thi gian nht nh sau khi vo Ch Ng su th h thng s p dng 88 cc hn ch cn li ca Ch Ng su thnh {@link android.os.PowerManager.WakeLock}, bo thc 89 {@link android.app.AlarmManager}, GPS, qut Wi-Fi. Cho d 90 mt s hoc tt c cc hn ch ca Ch Ng su c c p dng hay khng th h thng s nh thc 91 thit b trong cc khong thi gian bo tr ngn, trong cc ng dng c php 92 truy cp mng v c th thc thi mi tc v/ng b b tr hon. 93 </p> 94 95 96 <img src="{@docRoot}images/android-7.0/doze-diagram-2.png" alt="" id="figure2" /> 97 <p class="img-caption"> 98 <strong>Hnh 2.</strong> Minh ha cch Ch Ng su p dng cp th hai ca 99 cc hn ch hot ng h thng sau khi thit b c t mt ch trong mt khong thi gian nht nh. 100 </p> 101 102 <p> 103 Lu rng vic bt mn hnh hoc cm sc s a thit b thot Ch Ng su v 104 loi b cc hn ch x l ny. Hnh vi b sung ny khng 105 nh hng n cc khuyn ngh v cc thc hnh tt nht khi thch ng cho ng dng ca bn vi phin bn 106 Ch Ng su c a vo Android 6.0 (API mc 23) trc , nh m t trong 107 <a href="{@docRoot}training/monitoring-device-state/doze-standby.html"> 108 Ti u ha Ch Ng su v Ch Ch ca ng dng</a>. Bn nn tip tc 109 lm theo cc khuyn ngh , v d nh s dng Google Cloud Messaging (GCM) 110 gi v nhn tin nhn, v hy tin hnh p dng cc bn cp nht nhn 111 hnh vi Ch Ng su b sung. 112 </p> 113 114 115 <h3 id="bg-opt">D n Svelte: Ti u ha Chy ngm</h3> 116 117 <p> 118 Android N g b ba truyn pht khng biu th gip ti u ha c 119 vic s dng b nh ln tiu th in nng. Thay i ny l iu cn thit bi cc truyn pht 120 khng biu th thng xuyn khi ng cc ng dng c ng k lng nghe chng di 121 nn. G b cc truyn pht ny c th em li li ch ln i vi hiu nng 122 ca thit b v tri nghim ngi dng. 123 </p> 124 125 <p> 126 Cc thit b di ng thng xuyn gp phi nhng thay i v kt ni, chng hn nh khi di chuyn 127 gia Wi-Fi v d liu di ng. Hin ti, cc ng dng c th theo di nhng thay i v 128 kt ni bng cch ng k mt b thu truyn pht cho {@link 129 android.net.ConnectivityManager#CONNECTIVITY_ACTION} khng biu th trong bn k khai 130 ca chng. V nhiu ng dng ng k nhn truyn pht ny do mt hot ng chuyn mng 131 duy nht cng c th khin tt c cc ng dng thc dy v x l truyn pht ngay 132 lp tc. 133 </p> 134 135 <p> 136 Tng t, cc ng dng c th ng k nhn truyn pht {@link 137 android.hardware.Camera#ACTION_NEW_PICTURE} v {@link 138 android.hardware.Camera#ACTION_NEW_VIDEO} khng biu th t cc ng dng khc, v d nh 139 ng dng Camera. Khi mt ngi dng chp nh vi ng dng Camera th nhng ng dng ny s thc dy 140 x l truyn pht . 141 </p> 142 143 <p> 144 loi b cc vn ny, Android N p dng cc cch ti u ha 145 sau: 146 </p> 147 148 <ul> 149 <li>ng dng nhm mc tiu Android N s khng nhn cc truyn pht {@link 150 android.net.ConnectivityManager#CONNECTIVITY_ACTION}, ngay c khi chng 151 c cc mc nhp ca trong bn k khai yu cu thng bo v cc s kin ny. Nhng ng dng ang chy 152 tin cnh vn c th lng nghe {@code CONNECTIVITY_CHANGE} qua 153 lung chnh ca chng nu yu cu thng bo bng {@link 154 android.content.BroadcastReceiver}. 155 </li> 156 157 <li>ng dng khng th gi hoc nhn cc truyn pht {@link 158 android.hardware.Camera#ACTION_NEW_PICTURE} hoc {@link 159 android.hardware.Camera#ACTION_NEW_VIDEO}. Cch ti u ny 160 tc ng n mi ng dng, khng ch cc ng dng nhm mc tiu Android N. 161 </li> 162 </ul> 163 164 <p> 165 Cc ln pht hnh Android sau ny c th loi b thm cc truyn pht khng biu th, cng 166 nh cc dch v chy ngm khng cn lin kt. V l do ny, bn nn trnh dng hoc 167 g b cc ph thuc trn cc b thu c khai bo trong bn k khai i vi cc truyn pht khng biu th, 168 cng nh cc dch v chy ngm. 169 </p> 170 171 <p> 172 Khun kh Android cung cp mt s gii php gim thiu s cn thit i vi 173 cc truyn pht khng biu th hoc cc dch v chy ngm ny. V d, API {@link 174 android.app.job.JobScheduler} cung cp mt c ch ln lch hiu qu 175 cho cc hot ng mng khi p ng cc iu kin c ch nh, v d nh kt ni ti mt 176 mng khng o lu lng. Bn cn c th s dng {@link 177 android.app.job.JobScheduler} phn ng li cc thay i i vi trnh cung cp ni dung. 178 </p> 179 180 <p> 181 bit thm thng tin v thay i hnh vi ny v cch thch ng ng dng ca bn, 182 hy xem <a href="{@docRoot}preview/features/background-optimization.html">Ti u ha 183 Chy ngm</a>. 184 </p> 185 186 187 <h2 id="perm">Thay i Quyn</h2> 188 189 <p> 190 Android N c cc thay i i vi quyn c th tc ng n ng dng ca bn, 191 bao gm cc quyn ti khon ngi dng v mt quyn mi cho php ghi vo 192 b nh ngoi. Sau y l phn tm tt cc quyn thay i trong 193 bn xem trc: 194 </p> 195 196 <ul> 197 <li>{@code GET_ACCOUNTS} (B loi b) 198 <p> 199 Quyn GET_ACCOUNTS nay b loi b. H thng s b qua quyn 200 ny i vi cc ng dng nhm mc tiu Android N. 201 </p> 202 </li> 203 204 </ul> 205 206 207 208 <h2 id="accessibility">Ci thin Tr nng</h2> 209 210 <p> 211 Android N c cc thay i nhm nng cao kh nng s dng 212 ca nn tng cho nhng ngi dng c th lc km hoc khim th. Nhn chung, cc thay i ny s khng 213 yu cu thay i m ngun trong ng dng ca bn. Tuy nhin, bn cn xem li 214 cc tnh nng ny v kim th chng vi ng dng ca mnh nh gi cc tc ng tim tng i vi tri nghim 215 ngi dng. 216 </p> 217 218 219 <h3 id="screen-zoom">Thu phng Mn hnh</h3> 220 221 <p> 222 Android N cho php ngi dng t <strong>Display size</strong> phng to 223 hoc thu nh tt c cc phn t trn mn hnh, nh ci thin tr nng ca thit b 224 cho nhng ngi dng c th lc km. Ngi dng khng th thu phng mn hnh vt qu chiu rng mn hnh 225 ti thiu <a href="http://developer.android.com/guide/topics/resources/providing-resources.html"> 226 sw320dp</a>. y l chiu rng ca Nexus 4, mt my in thoi ph bin c kch c trung bnh. 227 </p> 228 229 <div class="cols"> 230 231 <div class="col-6"> 232 <img src="{@docRoot}images/android-7.0/screen-zoom-1.png" alt="" height="XXX" id="figure1" /> 233 </div> 234 <div class="col-6"> 235 <img src="{@docRoot}images/android-7.0/screen-zoom-2.png" alt="" height="XXX" id="figure1" /> 236 </div> 237 238 </div> <!-- end cols --> 239 <p class="img-caption"> 240 <strong>Hnh 3.</strong> Mn hnh bn phi hin th tc ng ca 241 vic tng kch c Hin th ca mt thit b ang chy nh h thng Android N. 242 </p> 243 244 245 <p> 246 Khi mt ca thit b thay i, h thng s thng bo cho cc ng dng ang chy theo 247 nhng cch sau: 248 </p> 249 250 <ul> 251 <li>Nu mt ng dng nhm mc tiu mc API 23 hoc thp hn th h thng s t ng tt 252 tt c cc tin trnh chy ngm ca ng dng . iu ny c ngha l nu mt ngi dng ri khi 253 ng dng m mn hnh <em>Settings</em> v thay i 254 <strong>Display size</strong> th h thng s tt ng dng ging 255 nh trong trng hp thit b thiu b nh. Nu ng dng c bt k tin trnh no 256 chy tin cnh th h thng s thng bo cho cc tin trnh v thay i cu hnh nh 257 m t trong <a href="{@docRoot}guide/topics/resources/runtime-changes.html">X l 258 Thay i Thi gian chy</a>, ging nh hng ca thit b va thay i. 259 </li> 260 261 <li>Nu ng dng nhm mc tiu Android N th mi tin trnh ca n 262 (chy tin cnh v chy ngm) u c thng bo v thay i cu hnh nh 263 m t trong <a href="{@docRoot}guide/topics/resources/runtime-changes.html">X l 264 Thay i Thi gian chy</a>. 265 </li> 266 </ul> 267 268 <p> 269 Hu ht cc ng dng khng cn thc hin bt k thay i no h tr tnh nng ny, min l 270 cc ng dng tun theo cc thc hnh tt nht ca Android. Nhng iu c th cn kim tra: 271 </p> 272 273 <ul> 274 <li>Kim th ng dng ca bn trn mt thit b c chiu rng mn hnh <code><a href= 275 "{@docRoot}guide/topics/resources/providing-resources.html">sw320dp</a></code> 276 v m bo n hot ng ng. 277 </li> 278 279 <li>Khi cu hnh ca thit b thay i, hy cp nht mi thng tin c lu tr trong b m 280 ph thuc vo mt , v d nh cc nh bitmap c lu m hoc cc ti nguyn c ti t 281 mng. Hy kim tra cc thay i cu hnh khi ng dng khi phc t trng thi 282 tm dng. 283 <p class="note"> 284 <strong>Lu :</strong> Nu bn lu tr trong b m cc d liu ph thuc vo cu hnh th nn 285 thm siu d liu lin quan nh kch c 286 mn hnh ph hp hoc mt im nh cho d liu . Vic lu siu d liu ny cho php bn 287 quyt nh liu bn c cn phi lm mi d liu c lu tr m sau khi mt cu hnh 288 thay i. 289 </p> 290 </li> 291 292 <li>Trnh ch nh cc kch thc theo n v px v chng khng thay i t l theo 293 mt mn hnh. Thay vo , hy ch nh cc kch thc bng cc n v <a href="{@docRoot}guide/practices/screens_support.html">im nh 294 c lp vi mt </a> (<code>dp</code>). 295 </li> 296 </ul> 297 298 <h3 id="vision-settings">Ci t Kh nng nhn trong Trnh Thit lp</h3> 299 300 <p> 301 Android N c Ci t Kh nng nhn trn mn hnh Cho mng, ni ngi dng c th 302 thit lp cc ci t tr nng sau y trn mt thit b mi: 303 <strong>Magnification gesture</strong>, <strong>Font size</strong>, 304 <strong>Display size</strong> v <strong>TalkBack</strong>. Thay i ny 305 s tng kh nng thy c cc khim khuyt lin quan n nhng ci t mn hnh khc nhau. 306 nh gi tc ng ca tnh nng ny, bn c th kim th ng dng ca mnh vi cc 307 ci t sau c bt. Bn c th tm cc ci t ny trong <strong>Settings > 308 Accessibility</strong>. 309 </p> 310 311 <h2 id="ndk">Lin kt ng dng NDK vi Th vin Nn tng</h2> 312 313 <p> 314 Android N c cc thay i i vi khng gian tn ngn vic ti cc API khng cng khai. 315 Nu bn s dng NDK, bn ch nn s dng cc API cng khai t nn tng 316 Android. S dng cc API khng cng khai trong bn pht hnh chnh thc tip theo ca Android 317 c th khin ng dng b treo. 318 </p> 319 320 <p> 321 cnh bo cho bn vic s dng cc API khng cng khai, cc ng dng chy trn thit b 322 Android N s to ra mt li trong phn xut ra ca logcat khi mt ng dng gi mt API khng cng khai. 323 Li ny cng c hin th trn mn hnh thit b di dng thng bo h tr 324 nng cao nhn bit i vi tnh hung ny. Bn nn xem li m ca ng dng 325 g b phn s dng cc API nn tng khng cng khai v kim th k lng ng dng bng 326 mt thit b hoc b gi lp ca bn xem trc. 327 </p> 328 329 <p> 330 Nu ng dng ca bn ph thuc vo cc th vin ca nn tng, hy xem ti liu NDK bit 331 cc cch khc phc c bn nhm thay th cc API khng cng khai bng cc API cng khai tng ng. 332 Bn cng c th lin kt vi cc th vin ca nn tng m khng nhn bit ng dng, 333 c bit nu ng dng ca bn s dng th vin thuc mt phn ca nn tng (nh 334 <code>libpng</code>), nhng khng thuc NDK. Trong trng hp , hy m bo rng 335 tp APK ca bn c cha tt c cc tp .so bn d nh lin kt vi. 336 </p> 337 338 <p class="caution"> 339 <strong>Cn trng:</strong> Mt s th vin ca bn th ba c th lin kt vi cc API 340 khng cng khai. Nu ng dng ca bn s dng cc th vin ny th ng dng ca bn c th gp li sp khi ang chy 341 trong ln pht hnh Android chnh thc k tip. 342 </p> 343 344 <p> 345 Cc ng dng khng nn ph thuc vo hoc s dng cc th vin gc khng km theo trong 346 NDK bi v chng c th thay i hoc b g b khi khi pht hnh bn Android khc 347 thay cho bn c. Vic chuyn t OpenSSL sang BoringSSL l mt v d v s thay i . 348 Ngoi ra, cc thit b khc nhau c th cung cp cc mc tng thch khc nhau bi 349 khng c yu cu no v kh nng tng thch cho cc th vin nn tng khng i km vi 350 NDK. Nu bn buc phi s dng cc th vin khng thuc NDK trn cc thit b c, hy thc hin 351 qu trnh ti ph thuc vo mc Android API. 352 </p> 353 354 <p> 355 Nhm gip bn chn on cc loi vn ny, sau y l mt s li Java v NDK 356 v d bn c th gp phi khi c dng ng dng vi Android N: 357 </p> 358 359 <p>V d li Java:</p> 360 <pre class="no-pretty-print"> 361 java.lang.UnsatisfiedLinkError: dlopen failed: library "/system/lib/libcutils.so" 362 is not accessible for the namespace "classloader-namespace" 363 </pre> 364 365 <p>V d li NDK:</p> 366 <pre class="no-pretty-print"> 367 dlopen failed: cannot locate symbol "__system_property_get" referenced by ... 368 </pre> 369 370 371 <p> 372 Sau y l mt s cch khc phc i vi ng dng gp phi cc kiu li ny: 373 </p> 374 375 <ul> 376 <li>Vic s dng getJavaVM v getJNIEnv trong libandroid_runtime.so c th c thay 377 bng cc hm JNI chun: 378 <pre class="no-pretty-print"> 379 AndroidRuntime::getJavaVM -> GetJavaVM from <jni.h> 380 AndroidRuntime::getJNIEnv -> JavaVM::GetEnv or 381 JavaVM::AttachCurrentThread from <jni.h>. 382 </pre> 383 </li> 384 385 <li>Vic s dng k hiu {@code property_get} trong {@code libcutils.so} c th c 386 thay bng phng thc {@code __system_property_get} cng khai thay th. 387 lm iu ny, hy dng {@code __system_property_get} vi cu lnh include sau: 388 <pre> 389 #include <sys/system_properties.h> 390 </pre> 391 </li> 392 393 <li>Vic s dng k hiu {@code SSL_ctrl} trong {@code libcrypto.so} nn c 394 thay bng mt k hiu cc b ca ng dng. V d, bn nn lin kt tnh 395 {@code libcyrpto.a} trong tp {@code .so} hoc thm tham chiu 396 {@code libcrypto.so} ng ca ring mnh t BoringSSL hoc OpenSSL trong ng dng ca bn. 397 </li> 398 </ul> 399 400 <h2 id="afw">Android cho Cng vic</h2> 401 <p> 402 Android N c cc thay i cho ng dng nhm mc tiu Android cho Cng vic, bao gm 403 cc thay i i vi ci t chng ch, t li mt khu, qun l ngi dng 404 th hai v truy cp cc m nh danh ca thit b. Nu bn ang dng cc ng dng dnh cho 405 cc mi trng Android cho Cng vic th bn nn xem li cc thay i ny v chnh sa 406 ng dng ca mnh cho ph hp. 407 </p> 408 409 <ul> 410 <li>Bn phi ci t mt trnh ci t chng ch y thc trc khi DPC c th t 411 n. i vi c ng dng ngi s hu cu hnh v ng dng ngi s hu thit b nhm mc tiu SDK N, bn cn 412 ci t trnh ci t chng ch y thc trc khi trnh kim sot 413 chnh sch ca thit b (DPC) gi 414 <code>DevicePolicyManager.setCertInstallerPackage()</code>. Nu trnh ci t 415 cha c ci th h thng s a ra mt 416 <code>IllegalArgumentException</code>. 417 </li> 418 419 <li>Gi y cc hn ch t li mt khu cho ngi qun l thit b s p dng vi ngi s hu 420 cu hnh. Ngi qun l thit b khng th s dng 421 <code>DevicePolicyManager.resetPassword()</code> c na xa mt khu hoc thay i 422 cc mt khu t. Ngi qun l thit b vn c th t mt mt khu nhng ch 423 khi thit b khng c mt khu, m PIN hoc mu hnh. 424 </li> 425 426 <li>Ngi s hu thit b hoc cu hnh c th qun l cc ti khon ngay c khi cc hn ch c 427 t. Ngi s hu thit b v ngi s hu cu hnh c th gi cc API Qun l ti khon 428 ngay c khi cc hn ch ngi dng <code>DISALLOW_MODIFY_ACCOUNTS</code> c sn. 429 </li> 430 431 <li>Ngi s hu thit b c th qun l nhng ngi dng th cp d dng hn. Khi mt thit b 432 ang chy trong ch ngi s hu thit b, hn ch <code>DISALLOW_ADD_USER</code> 433 s c t t ng. iu ny ngn ngi dng to cc ngi dng 434 th cp khng c qun l. Ngoi ra, cc phng thc <code>CreateUser()</code> v 435 <code>createAndInitial()</code> b loi b; phng thc 436 <code>DevicePolicyManager.createAndManageUser()</code> mi s thay th chng. 437 </li> 438 439 <li>Ngi s hu thit b c th truy cp cc m nh danh ca thit b. Mt ngi s hu thit b c th truy cp 440 a ch MAC ca Wi-Fi ca mt thit b, bng phng thc 441 <code>DevicePolicyManagewr.getWifiMacAddress()</code>. Nu Wi-Fi cha tng 442 c bt trn thit b th phng thc ny s tr v mt gi tr {@code null}. 443 </li> 444 </ul> 445 446 <p> 447 bit thm thng tin v cc thay i i vi Android cho Cng vic trong Android N, hy xem 448 <a href="{@docRoot}preview/features/afw.html">Cp nht ca Android cho Cng vic</a>. 449 </p> 450 451 <h2 id="other">Cc im quan trng khc</h2> 452 453 <ul> 454 <li>Khi mt ng dng ang chy trn Android N nhng nhm mc tiu mc API thp hn, 455 v ngi dng thay i kch c hin th th tin trnh ca ng dng s b tt. ng dng 456 phi c kh nng x l tnh hung ny mt cch tinh t. Nu khng, ng dng s b li sp 457 khi ngi dng khi phc n t mc Gn y. 458 459 <p> 460 Bn cn kim th ng dng m bo 461 rng hnh vi ny khng xy ra. 462 Bn c th thc hin iu bng cch gy ra mt li sp ging vy 463 khi tt ng dng mt cch th cng qua DDMS. 464 </p> 465 466 <p> 467 Cc ng dng nhm mc tiu N v cao hn s khng b tt t ng khi c cc thay i v mt ; 468 tuy nhin, chng c th phn hi kh khn i vi cc thay i cu hnh. 469 </p> 470 </li> 471 472 <li> 473 Cc ng dng trn Android N cn c kh nng x l cc thay i cu hnh mt cch tinh t, 474 v khng b li sp trong cc ln khi ng tip theo. Bn c th kim tra hnh vi ng dng 475 bng cch thay i kch c phng ch (<strong>Setting</strong> > 476 <strong>Display</strong> > <strong>Font size</strong>), v sau khi phc 477 ng dng t mc Gn y. 478 </li> 479 </ul> 480 481