Home | History | Annotate | Download | only in articles
      1 page.title=Khi ng Trc tip
      2 page.keywords=preview,sdk,direct boot
      3 page.tags=androidn
      4 page.image=images/cards/card-nyc_2x.jpg
      5 
      6 @jd:body
      7 
      8 <div id="tb-wrapper">
      9 <div id="tb">
     10   <h2>Trong ti liu ny</h2>
     11   <ol>
     12     <li><a href="#run">Yu cu Truy cp  Chy trong qu trnh Khi ng Trc tip</a></li>
     13     <li><a href="#access">Truy cp B nh Lu tr M ha ca Thit b</a></li>
     14     <li><a href="#notification">Nhn thng bo M kha ca Ngi dng</a></li>
     15     <li><a href="#migrating">Chuyn nhp D liu C sn</a></li>
     16     <li><a href="#testing">Kim th ng dng Nhn bit M ha ca bn</a></li>
     17   </ol>
     18 </div>
     19 </div>
     20 
     21 <p>Android N chy trong ch  <i>Khi ng Trc tip</i> an ton
     22 khi thit b  c bt ngun nhng ngi dng cha m kha
     23 thit b.  h tr ch  ny, h thng cung cp hai v tr lu tr d liu:</p>
     24 
     25 <ul>
     26 <li><i>Lu tr m ha thng tin xc thc</i>, l v tr lu tr mc nh
     27 v ch kh dng sau khi ngi dng  m kha thit b.</li>
     28 <li><i>Lu tr m ha thit b</i>, l v tr lu tr kh dng cho c
     29 ch  Khi ng Trc tip v sau khi ngi dng  m kha thit b.</li>
     30 </ul>
     31 
     32 <p>Theo mc nh, cc ng dng khng chy trong qu trnh ch  Khi ng Trc tip.
     33 Nu ng dng ca bn cn thc hin hnh ng trong ch  Khi ng Trc tip th bn c th ng k
     34 cc thnh phn ng dng cn chy trong ch  ny. Mt s trng hp s dng ph bin
     35 cho cc ng dng cn chy trong ch  Khi ng Trc tip gm:</p>
     36 
     37 <ul>
     38 <li>ng dng c thng bo theo lch, nh ng dng
     39 ng h bo thc.</li>
     40 <li>ng dng cung cp cc thng bo quan trng cho ngi dng nh ng dng SMS.</li>
     41 <li>ng dng cung cp cc dch v tr nng nh Talkback.</li>
     42 </ul>
     43 
     44 <p>Nu ng dng ca bn cn truy cp d liu khi ang chy trong ch  Khi ng Trc tip th hy s dng
     45 lu tr m ha thit b. B nh lu tr c m ha ca thit b c cha d liu
     46 c m ha bng mt kha ch kh dng sau khi thit b  thc hin mt
     47 ln khi ng c xc thc thnh cng.</p>
     48 
     49 <p>i vi d liu cn c m ha bng mt kha lin kt ti thng tin xc thc
     50 ca ngi dng nh m PIN hoc mt khu th hy s dng b nh lu tr m ha thng tin xc thc.
     51 B nh lu tr m ha thng tin xc thc ch kh dng sau khi ngi dng  m kha
     52 thnh cng thit b, tnh n khi ngi dng khi ng li thit b mt ln na. Nu
     53 ngi dng m mn hnh kha sau khi m kha thit b th thao tc ny khng kha
     54 b nh m ha thng tin xc thc.</p>
     55 
     56 <h2 id="run">Yu cu Truy cp  Chy trong qu trnh Khi ng Trc tip</h2>
     57 
     58 <p>Cc ng dng phi ng k cc thnh phn ca chng vi h thng trc khi chng
     59 c th chy trong ch  Khi ng Trc tip hoc truy cp b nh lu tr
     60 m ha thit b. ng dng ng k vi h thng bng cch nh du cc thnh phn l
     61 <i>nhn bit m ha</i>.  nh du cc thnh phn ca bn l nhn bit m ha, hy t thuc tnh
     62 <code>android:encryptionAware</code> thnh true trong bn k khai.<p>
     63 
     64 <p>Cc thnh phn nhn bit m ha c th ng k  nhn mt thng ip truyn pht
     65 <code>LOCKED_BOOT_COMPLETED</code> t
     66 h thng khi thit b c khi ng li. Lc ny, b nh lu tr
     67 m ha thit b s kh dng v thnh phn ca bn c th thc thi cc tc v cn c
     68 chy trong ch  Khi ng Trc tip, nh kch hot bo thc  t.</p>
     69 
     70 <p>on m sau l mt v d v cch ng k mt
     71 {@link android.content.BroadcastReceiver} l nhn bit m ha v thm mt
     72 b lc  nh cho <code>LOCKED_BOOT_COMPLETED</code> trong bn k khai ca ng dng:</p>
     73 
     74 <pre>
     75 &lt;receiever
     76   android:encryptionAware="true" &gt;
     77   ...
     78   &lt;intent-filter&gt;
     79     &lt;action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" /&gt;
     80   &lt;/intent-filter&gt;
     81 &lt;/receiver&gt;
     82 </pre>
     83 
     84 <p>Khi ngi dng  m kha thit b th mi thnh phn c th truy cp
     85 c b nh lu tr m ha thit b ln b nh lu tr m ha thng tin xc thc.</p>
     86 
     87 <h2 id="access">Truy cp B nh Lu tr M ha ca Thit b</h2>
     88 
     89 <p> truy cp b nh lu tr m ha thit b, hy to mt thc th
     90 {@link android.content.Context} th hai bng cch gi
     91 <code>Context.createDeviceEncryptedStorageContext()</code>. Tt c cc lnh gi
     92 API b nh lu tr u s dng bi cnh ny  truy cp b nh lu tr m ha thit b.
     93 V d sau s truy cp b nh lu tr m ha ca thit b v m mt tp
     94 d liu ng dng c sn:</p>
     95 
     96 <pre>
     97 Context directBootContext = Context.createDeviceEncryptedStorageContext();
     98 // Access appDataFilename that lives in device encrypted storage
     99 FileInputStream inStream = directBootContext.openFileInput(appDataFilename);
    100 // Use inStream to read content...
    101 </pre>
    102 
    103 <p>Ch s dng b nh lu tr m ha ca thit b
    104 cho thng tin phi truy cp c trong ch  Khi ng Trc tip.
    105 Khng s dng b nh lu tr m ha ca thit b lm b lu tr m ha cho mc ch chung.
    106 i vi thng tin c nhn ca ngi dng, hoc d liu c m ha khng cn thit
    107 trong ch  Khi ng Trc tip th hy s dng b nh lu tr m ha thng tin xc thc.</p>
    108 
    109 <h2 id="notification">Nhn thng bo M kha ca Ngi dng</h2>
    110 
    111 <p>Mt khi ngi dng m kha thit b sau khi khi ng li, ng dng ca bn c th chuyn sang
    112 truy cp b nh lu tr m ha thng tin xc thc v s dng cc dch v thng thng ca h thng
    113 ph thuc vo thng tin xc thc ngi dng.</p>
    114 
    115 <p> nhn thng bo khi ngi dng m kha thit b sau khi khi ng li,
    116 hy ng k mt {@link android.content.BroadcastReceiver} t mt thnh phn ang chy
    117  lng nghe thng bo <code>ACTION_USER_UNLOCKED</code>. Hoc bn c th
    118 nhn thng bo c sn {@link android.content.Intent#ACTION_BOOT_COMPLETED
    119 ACTION_BOOT_COMPLETED} lc ny s ch bo thit b  khi ng xong v
    120 ngi dng  m kha thit b.</p>
    121 
    122 <p>Bn c th truy vn trc tip  bit ngi dng  m kha thit b hay cha bng cch gi
    123 <code>UserManager.isUserUnlocked()</code>.</p>
    124 
    125 <h2 id="migrating">Chuyn nhp D liu C sn</h2>
    126 
    127 <p>Nu ngi dng cp nht thit b ca h  s dng ch  Khi ng Trc tip th bn c th c d liu
    128 hin hu cn c chuyn nhp sang b nh lu tr m ha ca thit b. S dng
    129 <code>Context.migrateSharedPreferencesFrom()</code> v
    130 <code>Context.migrateDatabaseFrom()</code>  chuyn nhp d liu v ty chn v c s d liu
    131 gia b nh lu tr m ha thng tin xc thc v b nh lu tr m ha thit b.</p>
    132 
    133 <p>Hy phn on hp l nht khi quyt nh d liu no nn chuyn nhp t b nh lu tr m ha
    134 thng tin xc thc sang b nh lu tr m ha thit b. Bn khng nn di chuyn
    135 thng tin c nhn ca ngi dng nh mt khu hoc cc m thng bo cp quyn sang
    136 b nh lu tr m ha thit b. Trong mt s trng hp, bn c th cn qun l
    137 cc b d liu ring bit trong hai b lu tr m ha.</p>
    138 
    139 <h2 id="testing">Kim th ng dng Nhn bit M ha ca bn</h2>
    140 
    141 <p>Kim th ng dng nhn bit m ha bng cch s dng ch  Khi ng Trc tip mi. C
    142 hai cch  kch hot Khi ng Trc tip.</p>
    143 
    144 <p class="caution"><strong>Cn trng:</strong> Kch hot Khi ng Trc tip
    145 s xa sch mi d liu ngi dng trn thit b.</p>
    146 
    147 <p>Trn cc thit b c h tr c Android N c ci t, kch hot
    148 Khi ng Trc tip bng cch thc hin mt trong cc thao tc sau:</p>
    149 
    150 <ul>
    151 <li>Trn thit b, bt <b>Developer options</b> nu cha hy bt bng cch
    152 vo <b>Settings &gt; About phone</b> v nhn vo <b>Build number</b>
    153 by ln. Khi mn hnh ty chn cho nh pht trin kh dng, hy vo
    154 <b>Settings &gt; Developer options</b> v chn
    155 <b>Convert to file encryption</b>.</li>
    156 <li>S dng cc lnh shell adb sau  kch hot ch  Khi ng Trc tip:
    157 <pre class="no-pretty-print">
    158 $ adb reboot-bootloader
    159 $ fastboot --wipe-and-use-fbe
    160 </pre>
    161 </li>
    162 </ul>
    163 
    164 <p>Cng c mt ch  Khi ng Trc tip gi lp trong trng hp bn cn chuyn i
    165 cc ch  trn thit b th nghim. Ch nn s dng ch  Gi lp trong qu trnh
    166 pht trin v c th lm mt d liu.  kch hot ch  Khi ng Trc tip gi lp,
    167 hy t mu hnh kha cho thit b, chn "No thanks" nu c yu cu
    168  bt mn hnh khi ng bo mt khi t mt mu hnh kha v sau  s dng
    169 lnh shell adb sau:</p>
    170 
    171 <pre class="no-pretty-print">
    172 $ adb shell sm set-emulate-fbe true
    173 </pre>
    174 
    175 <p>Hy s dng lnh sau  tt ch  Khi ng Trc tip gi lp:</p>
    176 
    177 <pre class="no-pretty-print">
    178 $ adb shell sm set-emulate-fbe false
    179 </pre>
    180 
    181 <p>S dng cc lnh ny c th khin cho thit b khi ng li.</p>
    182