1 page.title=Ni dung C bn v Trnh cung cp Ni dung 2 @jd:body 3 <div id="qv-wrapper"> 4 <div id="qv"> 5 <!-- In this document --> 6 <h2>Trong ti liu ny</h2> 7 <ol> 8 <li> 9 <a href="#Basics">Tng quan</a> 10 <ol> 11 <li> 12 <a href="#ClientProvider">Truy cp mt trnh cung cp</a> 13 </li> 14 <li> 15 <a href="#ContentURIs">URI Ni dung</a> 16 </li> 17 </ol> 18 </li> 19 <li> 20 <a href="#SimpleQuery">Truy xut D liu t Trnh cung cp</a> 21 <ol> 22 <li> 23 <a href="#RequestPermissions">Yu cu quyn truy cp c</a> 24 </li> 25 <li> 26 <a href="#Query">Xy dng truy vn</a> 27 </li> 28 <li> 29 <a href="#DisplayResults">Hin th cc kt qu truy vn</a> 30 </li> 31 <li> 32 <a href="#GettingResults">Ly d liu t cc kt qu truy vn</a> 33 </li> 34 </ol> 35 </li> 36 <li> 37 <a href="#Permissions">Quyn ca Trnh cung cp Ni dung</a> 38 </li> 39 <li> 40 <a href="#Modifications">Chn, Cp nht, v Xa D liu</a> 41 <ol> 42 <li> 43 <a href="#Inserting">Chn d liu</a> 44 </li> 45 <li> 46 <a href="#Updating">Cp nht d liu</a> 47 </li> 48 <li> 49 <a href="#Deleting">Xa d liu</a> 50 </li> 51 </ol> 52 </li> 53 <li> 54 <a href="#DataTypes">Cc Kiu D liu ca Trnh cung cp</a> 55 </li> 56 <li> 57 <a href="#AltForms">Cc Hnh thc Truy cp Trnh cung cp Thay th</a> 58 <ol> 59 <li> 60 <a href="#Batch">Truy cp hng lot</a> 61 </li> 62 <li> 63 <a href="#Intents">Truy cp d liu thng qua nh</a> 64 </li> 65 </ol> 66 </li> 67 <li> 68 <a href="#ContractClasses">Cc Lp Hp ng</a> 69 </li> 70 <li> 71 <a href="#MIMETypeReference">Tham kho Kiu MIME</a> 72 </li> 73 </ol> 74 75 <!-- Key Classes --> 76 <h2>Lp kha</h2> 77 <ol> 78 <li> 79 {@link android.content.ContentProvider} 80 </li> 81 <li> 82 {@link android.content.ContentResolver} 83 </li> 84 <li> 85 {@link android.database.Cursor} 86 </li> 87 <li> 88 {@link android.net.Uri} 89 </li> 90 </ol> 91 92 <!-- Related Samples --> 93 <h2>Cc Mu Lin quan</h2> 94 <ol> 95 <li> 96 <a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/view/List2.html"> 97 Con chy (Danh b)</a> 98 </li> 99 <li> 100 <a href="{@docRoot}resources/samples/ApiDemos/src/com/example/android/apis/view/List7.html"> 101 Con chy (in thoi)</a> 102 </li> 103 </ol> 104 105 <!-- See also --> 106 <h2>Xem thm</h2> 107 <ol> 108 <li> 109 <a href="{@docRoot}guide/topics/providers/content-provider-creating.html"> 110 To mt Trnh cung cp Ni dung</a> 111 </li> 112 <li> 113 <a href="{@docRoot}guide/topics/providers/calendar-provider.html"> 114 Trnh cung cp Lch</a> 115 </li> 116 </ol> 117 </div> 118 </div> 119 120 <!-- Intro paragraphs --> 121 <p> 122 Trnh cung cp ni dung qun l truy cp vo mt kho d liu tp trung. Trnh cung cp 123 l b phn ca mt ng dng Android, n thng cung cp UI ca chnh mnh lm vic cng 124 d liu. Tuy nhin, trnh cung cp ni dung c thit k ch yu cho cc ng dng khc 125 s dng, gip truy cp trnh cung cp bng cch s dng mt i tng my khch cung cp. Cng nhau, trnh cung cp 126 v my khch cung cp s mang n mt giao din nht qun, tiu chun cho d liu, giao din ny 127 cng ng thi x l truyn thng lin tin trnh v bo mt truy cp d liu. 128 </p> 129 <p> 130 Ch ny cp n nhng ni dung c bn sau y: 131 </p> 132 <ul> 133 <li>Cch trnh cung cp ni dung hot ng.</li> 134 <li>API bn s dng truy xut d liu t mt trnh cung cp ni dung.</li> 135 <li>API bn s dng chn, cp nht, hoc xa d liu trong mt trnh cung cp ni dung.</li> 136 <li>Cc tnh nng API khc to iu kin lm vic cng cc trnh cung cp.</li> 137 </ul> 138 139 <!-- Basics --> 140 <h2 id="Basics">Tng quan</h2> 141 <p> 142 Trnh cung cp ni dung trnh by d liu cho cc ng dng bn ngoi di dng mt hoc nhiu bng tng t 143 nh cc bng c tm thy trong mt c s d liu quan h. Mi hng th hin mt thc th ca mt s kiu d liu 144 m trnh cung cp thu thp, v mi ct trong hng th hin mt phn ring bit ca d liu c thu thp 145 i vi mt thc th. 146 </p> 147 <p> 148 V d, mt trong cc trnh cung cp tch hp trong nn tng Android l t in ngi dng, n 149 lu gi chnh t ca nhng t phi tiu chun m ngi dng mun gi li. Bng 1 minh ha 150 cch m d liu c th c trnh by trong bng ca trnh cung cp ny: 151 </p> 152 <p class="table-caption"> 153 <strong>Bng 1:</strong> Bng t in ngi dng mu. 154 </p> 155 <table id="table1" style="width: 50%;"> 156 <tr> 157 <th style="width:20%" align="center" scope="col">t</th> 158 <th style="width:20%" align="center" scope="col">id ng dng</th> 159 <th style="width:20%" align="center" scope="col">tn sut</th> 160 <th style="width:20%" align="center" scope="col">bn a</th> 161 <th style="width:20%" align="center" scope="col">_ID</th> 162 </tr> 163 <tr> 164 <td align="center" scope="row">mapreduce</td> 165 <td align="center">user1</td> 166 <td align="center">100</td> 167 <td align="center">en_US</td> 168 <td align="center">1</td> 169 </tr> 170 <tr> 171 <td align="center" scope="row">precompiler</td> 172 <td align="center">user14</td> 173 <td align="center">200</td> 174 <td align="center">fr_FR</td> 175 <td align="center">2</td> 176 </tr> 177 <tr> 178 <td align="center" scope="row">applet</td> 179 <td align="center">user2</td> 180 <td align="center">225</td> 181 <td align="center">fr_CA</td> 182 <td align="center">3</td> 183 </tr> 184 <tr> 185 <td align="center" scope="row">const</td> 186 <td align="center">user1</td> 187 <td align="center">255</td> 188 <td align="center">pt_BR</td> 189 <td align="center">4</td> 190 </tr> 191 <tr> 192 <td align="center" scope="row">int</td> 193 <td align="center">user5</td> 194 <td align="center">100</td> 195 <td align="center">en_UK</td> 196 <td align="center">5</td> 197 </tr> 198 </table> 199 <p> 200 Trong bng 1, mi hng th hin mt thc th ca mt t m c th khng thy c 201 trong t in chun. Mi ct th hin mt s d liu cho t , chng hn nh 202 t bn a c dng ln u cho t . Tiu ct l cc tn ct c lu gi trong 203 trnh cung cp. tham kho ti bn a ca mt hng, bn tham kho ti ct <code>locale</code> ca hng . i vi 204 trnh cung cp ny, ct <code>_ID</code> ng vai tr l ct "kha chnh" m 205 trnh cung cp t ng duy tr. 206 </p> 207 <p class="note"> 208 <strong>Lu :</strong> Trnh cung cp khng bt buc phi c mt kha chnh, v khng bt buc phi 209 s dng <code>_ID</code> lm tn ct ca mt kha chnh nu c kha. Tuy nhin, 210 nu bn mun gn kt d liu t mt trnh cung cp vi mt {@link android.widget.ListView}, mt trong cc 211 tn ct s phi l <code>_ID</code>. Yu cu ny c gii thch chi tit hn trong 212 phn <a href="#DisplayResults">Hin th cc kt qu truy vn</a>. 213 </p> 214 <h3 id="ClientProvider">Truy cp mt trnh cung cp</h3> 215 <p> 216 Mt ng dng truy cp d liu t mt trnh cung cp ni dung bng 217 mt i tng my khch {@link android.content.ContentResolver}. i tng ny c cc phng php gi 218 nhng phng php c tn ging nhau trong i tng trnh cung cp, mt thc th ca mt trong nhng lp con 219 c th ca {@link android.content.ContentProvider}. Cc phng php 220 {@link android.content.ContentResolver} cung cp cc chc nng 221 "CRUD" (to, truy xut, cp nht, v xa) c bn ca thit b lu tr lin tc. 222 </p> 223 <p> 224 i tng {@link android.content.ContentResolver} trong tin trnh 225 ca ng dng my khch v i tng {@link android.content.ContentProvider} trong ng dng m s hu 226 trnh cung cp s t ng x l truyn thng lin tin trnh. 227 {@link android.content.ContentProvider} cng ng vai tr nh mt lp rt gn gia kho d liu 228 ca n v biu din bn ngoi ca d liu di dng bng. 229 </p> 230 <p class="note"> 231 <strong>Lu :</strong> truy cp mt trnh cung cp, ng dng ca bn thng phi yu cu cc quyn 232 c th trong tp bn k khai ca mnh. iu ny c m t chi tit hn trong phn 233 <a href="#Permissions">Quyn ca Trnh cung cp Ni dung</a> 234 </p> 235 <p> 236 V d, c mt danh sch cc t v ni dung bn a ca chng t Trnh cung cp T in Ngi dng, 237 bn hy gi {@link android.content.ContentResolver#query ContentResolver.query()}. 238 Phng php {@link android.content.ContentResolver#query query()} s gi phng php 239 {@link android.content.ContentProvider#query ContentProvider.query()} c nh ngha bi 240 Trnh cung cp T in Ngi dng. Cc dng m sau th hin mt lnh gi 241 {@link android.content.ContentResolver#query ContentResolver.query()}: 242 <p> 243 <pre> 244 // Queries the user dictionary and returns results 245 mCursor = getContentResolver().query( 246 UserDictionary.Words.CONTENT_URI, // The content URI of the words table 247 mProjection, // The columns to return for each row 248 mSelectionClause // Selection criteria 249 mSelectionArgs, // Selection criteria 250 mSortOrder); // The sort order for the returned rows 251 </pre> 252 <p> 253 Bng 2 cho bit cc tham i ti 254 {@link android.content.ContentResolver#query 255 query(Uri,projection,selection,selectionArgs,sortOrder)} khp vi mt cu lnh SQL SELECT nh th no: 256 </p> 257 <p class="table-caption"> 258 <strong>Bng 2:</strong> Query() so vi truy vn SQL. 259 </p> 260 <table id="table2" style="width: 75%;"> 261 <tr> 262 <th style="width:25%" align="center" scope="col">tham i query()</th> 263 <th style="width:25%" align="center" scope="col">T kha/tham s SELECT</th> 264 <th style="width:50%" align="center" scope="col">Lu </th> 265 </tr> 266 <tr> 267 <td align="center"><code>Uri</code></td> 268 <td align="center"><code>FROM <em>table_name</em></code></td> 269 <td><code>Uri</code> nh x ti bng trong trnh cung cp c tn <em>table_name</em>.</td> 270 </tr> 271 <tr> 272 <td align="center"><code>projection</code></td> 273 <td align="center"><code><em>col,col,col,...</em></code></td> 274 <td> 275 <code>projection</code> l mt mng gm cc ct nn c a vo i vi mi hng 276 c truy xut. 277 </td> 278 </tr> 279 <tr> 280 <td align="center"><code>selection</code></td> 281 <td align="center"><code>WHERE <em>col</em> = <em>value</em></code></td> 282 <td><code>selection</code> quy nh cc tiu ch la chn hng.</td> 283 </tr> 284 <tr> 285 <td align="center"><code>selectionArgs</code></td> 286 <td align="center"> 287 (Khng c s tng ng chnh xc. Cc tham i la chn s thay th cc ch dnh sn <code>?</code> trong 288 mnh la chn.) 289 </td> 290 </tr> 291 <tr> 292 <td align="center"><code>sortOrder</code></td> 293 <td align="center"><code>ORDER BY <em>col,col,...</em></code></td> 294 <td> 295 <code>sortOrder</code> quy nh th t cc hng xut hin trong 296 {@link android.database.Cursor} c tr v. 297 </td> 298 </tr> 299 </table> 300 <h3 id="ContentURIs">URI Ni dung</h3> 301 <p> 302 <strong>URI ni dung</strong> l mt URI xc nh d liu trong mt trnh cung cp. URI ni dung 303 bao gm tn biu tng ca ton b trnh cung cp (<strong>quyn</strong> ca n) v mt 304 tn tr n mt bng (<strong>ng dn</strong>). Khi bn gi 305 mt phng php my khch truy cp mt bng trong mt trnh cung cp, URI ni dung cho bng l mt trong cc 306 tham i. 307 </p> 308 <p> 309 Trong cc dng m trc, hng s 310 {@link android.provider.UserDictionary.Words#CONTENT_URI} cha URI ni dung ca 311 bng "t" ca t in ngi dng. i tng {@link android.content.ContentResolver} 312 s phn tch quyn ca URI, v s dng n "gii quyt" trnh cung cp bng cch 313 so snh quyn vi mt bng h thng ca cc trnh cung cp bit. Khi , 314 {@link android.content.ContentResolver} c th phn phi cc tham i truy vn ti ng 315 trnh cung cp. 316 </p> 317 <p> 318 {@link android.content.ContentProvider} s dng phn ng dn ca URI ni dung nhm chn 319 bng truy cp. Trnh cung cp thng c mt <strong>ng dn</strong> cho mi bng m n hin ra. 320 </p> 321 <p> 322 Trong cc dng m trc, URI y cho bng "t" l: 323 </p> 324 <pre> 325 content://user_dictionary/words 326 </pre> 327 <p> 328 trong xu <code>user_dictionary</code> l quyn ca trnh cung cp, v 329 xu <code>words</code> l ng dn ca bng. Xu 330 <code>content://</code> (<strong>lc </strong>) s lun c mt, 331 v xc nh y l mt URI ni dung. 332 </p> 333 <p> 334 Nhiu trnh cung cp cho php bn truy cp mt hng n l trong mt bng bng cch ni mt gi tr ID 335 vi ui ca URI. V d, truy xut mt hng c <code>_ID</code> l 336 <code>4</code> t mt t in ngi dng, bn c th s dng URI ni dung ny: 337 </p> 338 <pre> 339 Uri singleUri = ContentUris.withAppendedId(UserDictionary.Words.CONTENT_URI,4); 340 </pre> 341 <p> 342 Bn thng s dng cc gi tr id khi bn truy xut mt tp hp cc hng, sau mun cp nht hoc xa 343 mt trong s chng. 344 </p> 345 <p class="note"> 346 <strong>Lu :</strong> Cc lp {@link android.net.Uri} v {@link android.net.Uri.Builder} 347 cha cc phng php thun tin xy dng i tng URI nh dng tt t cc xu. 348 {@link android.content.ContentUris} cha cc phng php thun tin ni cc gi tr id vi 349 mt URI. on m HTML trc s dng {@link android.content.ContentUris#withAppendedId 350 withAppendedId()} ni mt id vi URI ni dung T in Ngi dng. 351 </p> 352 353 354 <!-- Retrieving Data from the Provider --> 355 <h2 id="SimpleQuery">Truy xut D liu t Trnh cung cp</h2> 356 <p> 357 Phn ny m t cch truy xut d liu t mt trnh cung cp bng cch s dng Trnh cung cp T in Ngi dng 358 lm v d. 359 </p> 360 <p class="note"> 361 gii thch r, on m HTML trong phn ny gi 362 {@link android.content.ContentResolver#query ContentResolver.query()} trn "lung UI"". Tuy nhin, trong 363 m thc s, bn nn thc hin cc truy vn khng ng b trn mt lung ring. Mt cch lm 364 iu ny l s dng lp {@link android.content.CursorLoader}, n c m t chi tit hn 365 trong hng dn <a href="{@docRoot}guide/components/loaders.html"> 366 Trnh ti</a>. Bnh cnh , cc dng m ch l on m HTML; chng khng th hin mt ng dng 367 hon chnh. 368 </p> 369 <p> 370 truy xut d liu t mt trnh cung cp, hy lm theo cc bc c bn sau: 371 </p> 372 <ol> 373 <li> 374 Yu cu quyn truy cp c cho trnh cung cp. 375 </li> 376 <li> 377 nh ngha m gi mt truy vn ti trnh cung cp. 378 </li> 379 </ol> 380 <h3 id="RequestPermissions">Yu cu quyn truy cp c</h3> 381 <p> 382 truy xut d liu t mt trnh cung cp, ng dng ca bn cn "quyn truy cp c" cho 383 trnh cung cp. Bn khng th yu cu quyn ny trong thi gian chy; thay vo , bn phi ch nh rng 384 bn cn quyn ny trong bn k khai ca mnh bng cch s dng phn t 385 <code><a href="{@docRoot}guide/topics/manifest/uses-permission-element.html"><uses-permission></a></code> 386 v tn quyn chnh xc c nh ngha bi 387 trnh cung cp. Khi bn ch nh phn t ny trong bn k khai ca mnh, bn ang thc t ha "yu cu" quyn 388 ny cho ng dng ca mnh. Khi ngi dng ci t ng dng ca bn, h ngm hiu cp 389 yu cu ny. 390 </p> 391 <p> 392 tm tn chnh xc ca quyn truy cp c cho trnh cung cp bn ang s dng, cng nh 393 tn cho cc quyn truy cp khc c s dng bi trnh truy cp, hy xem trong ti liu 394 ca trnh cung cp. 395 </p> 396 <p> 397 Vai tr ca quyn trong vic truy cp cc trnh cung cp c m t chi tit hn trong phn 398 <a href="#Permissions">Quyn ca Trnh cung cp Ni dung</a>. 399 </p> 400 <p> 401 Trnh cung cp T in Ngi dng s nh ngha quyn 402 <code>android.permission.READ_USER_DICTIONARY</code> trong tp bn k khai ca n, v vy mt 403 ng dng mun c t trnh cung cp s phi yu cu quyn ny. 404 </p> 405 <!-- Constructing the query --> 406 <h3 id="Query">Xy dng truy vn</h3> 407 <p> 408 Bc tip theo trong vic truy xut d liu t mt trnh cung cp l xy dng mt truy vn. on m HTML u tin 409 ny nh ngha mt s bin cho vic truy cp Trnh cung cp T in Ngi dng: 410 </p> 411 <pre class="prettyprint"> 412 413 // A "projection" defines the columns that will be returned for each row 414 String[] mProjection = 415 { 416 UserDictionary.Words._ID, // Contract class constant for the _ID column name 417 UserDictionary.Words.WORD, // Contract class constant for the word column name 418 UserDictionary.Words.LOCALE // Contract class constant for the locale column name 419 }; 420 421 // Defines a string to contain the selection clause 422 String mSelectionClause = null; 423 424 // Initializes an array to contain selection arguments 425 String[] mSelectionArgs = {""}; 426 427 </pre> 428 <p> 429 on m HTML tip theo cho bit cch s dng 430 {@link android.content.ContentResolver#query ContentResolver.query()}, bng cch s dng Trnh cung cp T in 431 Ngi dng nh mt v d. Truy vn my khch trnh cung cp tng t nh mt truy vn SQL, v n cha mt 432 tp hp cc ct tr v, mt tp hp cc tiu ch la chn, v mt th t sp xp. 433 </p> 434 <p> 435 Tp hp cc ct m truy vn cn tr v c gi l <strong>d tho</strong> 436 (bin <code>mProjection</code>). 437 </p> 438 <p> 439 Biu thc ch nh cc hng cn truy xut s c chia thnh mt mnh la chn v 440 tham i la chn. Mnh la chn l s kt hp gia cc biu thc l-gic v biu thc Boolean, 441 tn ct, v gi tr (bin <code>mSelectionClause</code>). Nu bn ch nh 442 tham s thay th c <code>?</code> thay v mt gi tr, phng php truy vn s truy xut gi tr 443 t mng tham i la chn (bin <code>mSelectionArgs</code>). 444 </p> 445 <p> 446 Trong on m HTML tip theo, nu ngi dng khng in t th mnh la chn c t thnh 447 <code>null</code>, v truy vn tr v tt c cc t trong trnh cung cp. Nu ngi dng nhp 448 mt t, mnh la chn c t thnh <code>UserDictionary.Words.WORD + " = ?"</code> v 449 phn t u tin ca mng tham i la chn c t thnh t m ngi dng nhp. 450 </p> 451 <pre class="prettyprint"> 452 /* 453 * This defines a one-element String array to contain the selection argument. 454 */ 455 String[] mSelectionArgs = {""}; 456 457 // Gets a word from the UI 458 mSearchString = mSearchWord.getText().toString(); 459 460 // Remember to insert code here to check for invalid or malicious input. 461 462 // If the word is the empty string, gets everything 463 if (TextUtils.isEmpty(mSearchString)) { 464 // Setting the selection clause to null will return all words 465 mSelectionClause = null; 466 mSelectionArgs[0] = ""; 467 468 } else { 469 // Constructs a selection clause that matches the word that the user entered. 470 mSelectionClause = UserDictionary.Words.WORD + " = ?"; 471 472 // Moves the user's input string to the selection arguments. 473 mSelectionArgs[0] = mSearchString; 474 475 } 476 477 // Does a query against the table and returns a Cursor object 478 mCursor = getContentResolver().query( 479 UserDictionary.Words.CONTENT_URI, // The content URI of the words table 480 mProjection, // The columns to return for each row 481 mSelectionClause // Either null, or the word the user entered 482 mSelectionArgs, // Either empty, or the string the user entered 483 mSortOrder); // The sort order for the returned rows 484 485 // Some providers return null if an error occurs, others throw an exception 486 if (null == mCursor) { 487 /* 488 * Insert code here to handle the error. Be sure not to use the cursor! You may want to 489 * call android.util.Log.e() to log this error. 490 * 491 */ 492 // If the Cursor is empty, the provider found no matches 493 } else if (mCursor.getCount() < 1) { 494 495 /* 496 * Insert code here to notify the user that the search was unsuccessful. This isn't necessarily 497 * an error. You may want to offer the user the option to insert a new row, or re-type the 498 * search term. 499 */ 500 501 } else { 502 // Insert code here to do something with the results 503 504 } 505 </pre> 506 <p> 507 Truy vn ny tng t nh cu lnh SQL: 508 </p> 509 <pre> 510 SELECT _ID, word, locale FROM words WHERE word = <userinput> ORDER BY word ASC; 511 </pre> 512 <p> 513 Trong cu lnh SQL ny, tn ct thc t c s dng thay v cc hng s lp hp ng. 514 </p> 515 <h4 id="Injection">Bo v trc mc nhp c hi</h4> 516 <p> 517 Nu d liu c qun l bi trnh cung cp ni dung nm trong mt c s d liu SQL, vic in d liu khng c tin cy t bn ngoi 518 vo cc cu lnh SQL th c th dn n tim li SQL. 519 </p> 520 <p> 521 Hy xt mnh la chn sau: 522 </p> 523 <pre> 524 // Constructs a selection clause by concatenating the user's input to the column name 525 String mSelectionClause = "var = " + mUserInput; 526 </pre> 527 <p> 528 Nu bn lm vy, bn ang cho php ngi dng ghp ni SQL c hi ln cu lnh SQL ca mnh. 529 V d, ngi dng c th in "nothing; DROP TABLE *;" cho <code>mUserInput</code>, lm vy 530 s dn n mnh la chn <code>var = nothing; DROP TABLE *;</code>. Do 531 mnh la chn c coi nh mt cu lnh SQL, iu ny c th khin trnh cung cp xa tt c 532 bng trong c s d liu SQLite c bn (tr khi trnh cung cp c thit lp bt nhng ln th 533 <a href="http://en.wikipedia.org/wiki/SQL_injection">tim li SQL</a>). 534 </p> 535 <p> 536 trnh vn ny, hy s dng mt mnh la chn m s dng <code>?</code> lm tham s 537 thay th c v mt mng cc tham i la chn ring. Khi bn lm nh vy, mc nhp ca ngi dng 538 c gn kt trc tip vi truy vn thay v c gii ngha nh mt phn ca cu lnh SQL. 539 V n khng c coi nh SQL, mc nhp ca ngi dng khng th tim li SQL c hi. Thay v s dng 540 ghp ni in mc nhp ca ngi dng, hy s dng mnh la chn ny: 541 </p> 542 <pre> 543 // Constructs a selection clause with a replaceable parameter 544 String mSelectionClause = "var = ?"; 545 </pre> 546 <p> 547 Thit lp mng cc tham i la chn nh sau: 548 </p> 549 <pre> 550 // Defines an array to contain the selection arguments 551 String[] selectionArgs = {""}; 552 </pre> 553 <p> 554 t mt gi tr trong mng cc tham i la chn nh sau: 555 </p> 556 <pre> 557 // Sets the selection argument to the user's input 558 selectionArgs[0] = mUserInput; 559 </pre> 560 <p> 561 Mnh la chn m s dng <code>?</code> nh mt tham s thay th c v mt mng 562 cc tham i la chn l cch c u tin ch nh mt la chn, ngay c khi trnh cung cp khng 563 c da trn c s d liu SQL. 564 </p> 565 <!-- Displaying the results --> 566 <h3 id="DisplayResults">Hin th cc kt qu truy vn</h3> 567 <p> 568 Phng php my khch {@link android.content.ContentResolver#query ContentResolver.query()} lun tr v 569 mt {@link android.database.Cursor} cha cc ct c ch nh bi d tho ca 570 truy vn cho cc hng khp vi cc tiu ch la chn ca truy vn. Mt i tng 571 {@link android.database.Cursor} cung cp truy cp c ngu nhin vo cc hng v ct m n 572 cha. Bng cch s dng phng php {@link android.database.Cursor}, bn c th lp li cc hng trong 573 kt qu, xc nh kiu d liu ca tng ct, ly d liu ra khi ct, v kim tra cc tnh cht khc 574 ca kt qu. Mt s trin khai {@link android.database.Cursor} s t ng 575 cp nht i tng khi d liu ca trnh cung cp thay i, hoc kch khi cc phng php trong mt i tng quan st 576 khi {@link android.database.Cursor} thay i, hoc c hai. 577 </p> 578 <p class="note"> 579 <strong>Lu :</strong> Mt trnh cung cp c th hn ch truy cp vo cc ct da trn tnh cht ca 580 i tng thc hin truy vn. V d, Trnh cung cp Danh b hn ch truy cp i vi mt s ct cho 581 cc trnh iu hp ng b, v th n s khng tr chng v mt hot ng hay dch v. 582 </p> 583 <p> 584 Nu khng hng no khp vi cc tiu ch la chn, trnh cung cp 585 s tr v mt i tng {@link android.database.Cursor} m trong 586 {@link android.database.Cursor#getCount Cursor.getCount()} bng 0 (con chy trng). 587 </p> 588 <p> 589 Nu xy ra mt li ni b, cc kt qu ca truy vn s ph thuc vo trnh cung cp c th. N c th 590 chn tr v <code>null</code>, hoc n c th a ra mt li {@link java.lang.Exception}. 591 </p> 592 <p> 593 Do {@link android.database.Cursor} l mt "danh sch" hng, mt cch hay hin th 594 ni dung ca mt {@link android.database.Cursor} l lin kt n vi mt {@link android.widget.ListView} 595 thng qua mt {@link android.widget.SimpleCursorAdapter}. 596 </p> 597 <p> 598 on m HTML sau tip tc t on m HTML trc. N to mt i tng 599 {@link android.widget.SimpleCursorAdapter} cha {@link android.database.Cursor} 600 c truy xut bi truy vn, v t i tng ny thnh trnh iu hp cho mt 601 {@link android.widget.ListView}: 602 </p> 603 <pre class="prettyprint"> 604 // Defines a list of columns to retrieve from the Cursor and load into an output row 605 String[] mWordListColumns = 606 { 607 UserDictionary.Words.WORD, // Contract class constant containing the word column name 608 UserDictionary.Words.LOCALE // Contract class constant containing the locale column name 609 }; 610 611 // Defines a list of View IDs that will receive the Cursor columns for each row 612 int[] mWordListItems = { R.id.dictWord, R.id.locale}; 613 614 // Creates a new SimpleCursorAdapter 615 mCursorAdapter = new SimpleCursorAdapter( 616 getApplicationContext(), // The application's Context object 617 R.layout.wordlistrow, // A layout in XML for one row in the ListView 618 mCursor, // The result from the query 619 mWordListColumns, // A string array of column names in the cursor 620 mWordListItems, // An integer array of view IDs in the row layout 621 0); // Flags (usually none are needed) 622 623 // Sets the adapter for the ListView 624 mWordList.setAdapter(mCursorAdapter); 625 </pre> 626 <p class="note"> 627 <strong>Lu :</strong> li {@link android.widget.ListView} bng mt 628 {@link android.database.Cursor}, con chy phi cha mt ct c tn <code>_ID</code>. 629 V iu ny, truy vn c hin lc trc truy xut ct <code>_ID</code> cho 630 bng "t" mc d {@link android.widget.ListView} khng hin th n. 631 Hn ch ny cng gii thch l do ti sao phn ln trnh cung cp u c mt ct <code>_ID</code> cho mi 632 bng ca n. 633 </p> 634 635 <!-- Getting data from query results --> 636 <h3 id="GettingResults">Ly d liu t cc kt qu truy vn</h3> 637 <p> 638 Thay v ch hin th cc kt qu truy vn, bn c th s dng chng cho cc tc v khc. V 639 d, bn c th truy xut chnh t t mt t in ngi dng, ri sau tm kim t trong 640 cc trnh cung cp khc. lm iu ny, bn lp li cc hng trong {@link android.database.Cursor}: 641 </p> 642 <pre class="prettyprint"> 643 644 // Determine the column index of the column named "word" 645 int index = mCursor.getColumnIndex(UserDictionary.Words.WORD); 646 647 /* 648 * Only executes if the cursor is valid. The User Dictionary Provider returns null if 649 * an internal error occurs. Other providers may throw an Exception instead of returning null. 650 */ 651 652 if (mCursor != null) { 653 /* 654 * Moves to the next row in the cursor. Before the first movement in the cursor, the 655 * "row pointer" is -1, and if you try to retrieve data at that position you will get an 656 * exception. 657 */ 658 while (mCursor.moveToNext()) { 659 660 // Gets the value from the column. 661 newWord = mCursor.getString(index); 662 663 // Insert code here to process the retrieved word. 664 665 ... 666 667 // end of while loop 668 } 669 } else { 670 671 // Insert code here to report an error if the cursor is null or the provider threw an exception. 672 } 673 </pre> 674 <p> 675 Trin khai {@link android.database.Cursor} s cha mt vi phng php get" 676 truy xut cc kiu d liu khc nhau t i tng. V d, on m HTML trc 677 s dng {@link android.database.Cursor#getString getString()}. Chng cng c mt phng php 678 {@link android.database.Cursor#getType getType()} tr v mt gi tr cho bit 679 kiu d liu ca ct. 680 </p> 681 682 683 <!-- Requesting permissions --> 684 <h2 id="Permissions">Quyn ca Trnh cung cp Ni dung</h2> 685 <p> 686 ng dng ca mt trnh cung cp c th ch nh cc quyn m ng dng khc c th c 687 truy cp d liu ca trnh cung cp . Nhng quyn ny m bo rng ngi dng bit mt ng dng 688 s c gng truy cp d liu no. Da trn cc yu cu ca trnh cung cp, cc ng dng khc 689 yu cu quyn m chng cn truy cp trnh d liu. Ngi dng cui thy cc quyn 690 c yu cu khi h ci t ng dng. 691 </p> 692 <p> 693 Nu ng dng ca mt trnh cung cp khng ch nh bt k quyn no, khi cc ng dng khc khng c 694 quyn truy cp d liu ca trnh cung cp. Tuy nhin, cc thnh phn trong ng dng ca trnh cung cp lun c 695 y quyn truy nhp c v ghi, khng ph thuc vo cc quyn c ch nh. 696 </p> 697 <p> 698 Nh lu , Trnh cung cp T in Ngi dng s yu cu 699 quyn <code>android.permission.READ_USER_DICTIONARY</code> truy xut d liu t n. 700 Trnh cung cp c quyn <code>android.permission.WRITE_USER_DICTIONARY</code> 701 ring chn, cp nht, hoc xa d liu. 702 </p> 703 <p> 704 nhn cc quyn cn truy cp mt trnh cung cp, ng dng yu cu chng bng mt phn t 705 <code><a href="{@docRoot}guide/topics/manifest/uses-permission-element.html"><uses-permission></a></code> 706 trong tp bn k khai ca n. Khi Trnh qun l Gi Android ci t cc ng dng, ngi dng 707 phi ph chun tt c quyn m ng dng yu cu. Nu ngi dng ph chun tt c quyn, khi 708 Trnh qun l Gi s tip tc ci t; nu ngi dng khng ph chun chng, Trnh qun l Gi s 709 hy b vic ci t. 710 </p> 711 <p> 712 Phn t 713 <code><a href="{@docRoot}guide/topics/manifest/uses-permission-element.html"><uses-permission></a></code> 714 sau yu cu quyn truy cp c vo Trnh cung cp T in Ngi dng: 715 </p> 716 <pre> 717 <uses-permission android:name="android.permission.READ_USER_DICTIONARY"> 718 </pre> 719 <p> 720 Tc ng ca cc quyn ti vic truy cp trnh cung cp c gii thch chi tit hn trong 721 hng dn <a href="{@docRoot}guide/topics/security/security.html">Bo mt v Quyn</a>. 722 </p> 723 724 725 <!-- Inserting, Updating, and Deleting Data --> 726 <h2 id="Modifications">Chn, Cp nht, v Xa D liu</h2> 727 <p> 728 Ging nh cch bn truy xut d liu t mt trnh cung cp, bn cng c th s dng tng tc gia 729 mt my khch cung cp v {@link android.content.ContentProvider} ca trnh cung cp sa i d liu. 730 Bn gi mt phng php {@link android.content.ContentResolver} vi cc tham i c chuyn sang 731 phng php {@link android.content.ContentProvider} tng ng. Trnh cung cp v 732 my khch cung cp s t ng x l bo mt v truyn thng lin tin trnh. 733 </p> 734 <h3 id="Inserting">Chn d liu</h3> 735 <p> 736 chn d liu vo mt trnh cung cp, bn gi phng php 737 {@link android.content.ContentResolver#insert ContentResolver.insert()} 738 . Phng php ny chn mt hng mi vo trnh cung cp v tr v mt URI ni dung cho hng . 739 on m HTML ny cho bit cch chn mt t mi vo Trnh cung cp T in Ngi dng: 740 </p> 741 <pre class="prettyprint"> 742 // Defines a new Uri object that receives the result of the insertion 743 Uri mNewUri; 744 745 ... 746 747 // Defines an object to contain the new values to insert 748 ContentValues mNewValues = new ContentValues(); 749 750 /* 751 * Sets the values of each column and inserts the word. The arguments to the "put" 752 * method are "column name" and "value" 753 */ 754 mNewValues.put(UserDictionary.Words.APP_ID, "example.user"); 755 mNewValues.put(UserDictionary.Words.LOCALE, "en_US"); 756 mNewValues.put(UserDictionary.Words.WORD, "insert"); 757 mNewValues.put(UserDictionary.Words.FREQUENCY, "100"); 758 759 mNewUri = getContentResolver().insert( 760 UserDictionary.Word.CONTENT_URI, // the user dictionary content URI 761 mNewValues // the values to insert 762 ); 763 </pre> 764 <p> 765 D liu cho hng mi i vo trong mt i tng {@link android.content.ContentValues} n l, i tng ny 766 c dng tng t nh con chy mt hng. Cc ct trong i tng ny khng cn c 767 cng kiu d liu, v nu hon ton khng mun ch nh mt gi tr, bn c th t mt ct 768 thnh <code>null</code> bng cch s dng {@link android.content.ContentValues#putNull ContentValues.putNull()}. 769 </p> 770 <p> 771 on m HTML khng thm ct <code>_ID</code> v ct ny c t ng 772 duy tr. Trnh cung cp s gn mt gi tr duy nht <code>_ID</code> cho mi hng c 773 thm. Cc trnh cung cp thng s dng gi tr ny lm kha chnh ca bng. 774 </p> 775 <p> 776 URI ni dung c tr v trong <code>newUri</code> s xc nh hng mi thm, c 777 nh dng nh sau: 778 </p> 779 <pre> 780 content://user_dictionary/words/<id_value> 781 </pre> 782 <p> 783 <code><id_value></code> l ni dung ca <code>_ID</code> cho hng mi. 784 Hu ht cc trnh cung cp u c th t ng pht hin dng URI ni dung ny ri thc hin thao tc c yu cu 785 trn hng c th . 786 </p> 787 <p> 788 nhn gi tr <code>_ID</code> t {@link android.net.Uri} c tr v, hy gi 789 {@link android.content.ContentUris#parseId ContentUris.parseId()}. 790 </p> 791 <h3 id="Updating">Cp nht d liu</h3> 792 <p> 793 cp nht mt hng, bn s dng mt i tng {@link android.content.ContentValues} vi cc gi tr 794 c cp nht ging nh cch bn lm vi vic chn, v cc tiu ch la chn ging nh cch bn lm vi truy vn. 795 Phng php my khch m bn s dng l 796 {@link android.content.ContentResolver#update ContentResolver.update()}. Bn ch cn thm 797 cc gi tr vo i tng {@link android.content.ContentValues} cho cc ct m bn ang cp nht. Nu bn 798 mun xa cc ni dung ca mt ct, hy t gi tr thnh <code>null</code>. 799 </p> 800 <p> 801 on m HTML sau thay i tt c hng vi ct bn a c ngn ng "en" thnh ct 802 c bn a l <code>null</code>. Gi tr tr v l s hng c cp nht: 803 </p> 804 <pre> 805 // Defines an object to contain the updated values 806 ContentValues mUpdateValues = new ContentValues(); 807 808 // Defines selection criteria for the rows you want to update 809 String mSelectionClause = UserDictionary.Words.LOCALE + "LIKE ?"; 810 String[] mSelectionArgs = {"en_%"}; 811 812 // Defines a variable to contain the number of updated rows 813 int mRowsUpdated = 0; 814 815 ... 816 817 /* 818 * Sets the updated value and updates the selected words. 819 */ 820 mUpdateValues.putNull(UserDictionary.Words.LOCALE); 821 822 mRowsUpdated = getContentResolver().update( 823 UserDictionary.Words.CONTENT_URI, // the user dictionary content URI 824 mUpdateValues // the columns to update 825 mSelectionClause // the column to select on 826 mSelectionArgs // the value to compare to 827 ); 828 </pre> 829 <p> 830 Bn cng nn thanh lc thng tin u vo ca ngi dng khi gi 831 {@link android.content.ContentResolver#update ContentResolver.update()}. tm hiu thm v 832 iu ny, hy c phn <a href="#Injection">Bo v trc mc nhp c hi</a>. 833 </p> 834 <h3 id="Deleting">Xa d liu</h3> 835 <p> 836 Xa hng tng t nh truy xut d liu hng: bn ch nh cc tiu ch la chn cho hng 837 m bn mun xa v phng php my khch tr v s hng c xa. 838 on m HTML sau xa cc hng c appid khp vi "user". Phng php s tr v 839 s hng c xa. 840 </p> 841 <pre> 842 843 // Defines selection criteria for the rows you want to delete 844 String mSelectionClause = UserDictionary.Words.APP_ID + " LIKE ?"; 845 String[] mSelectionArgs = {"user"}; 846 847 // Defines a variable to contain the number of rows deleted 848 int mRowsDeleted = 0; 849 850 ... 851 852 // Deletes the words that match the selection criteria 853 mRowsDeleted = getContentResolver().delete( 854 UserDictionary.Words.CONTENT_URI, // the user dictionary content URI 855 mSelectionClause // the column to select on 856 mSelectionArgs // the value to compare to 857 ); 858 </pre> 859 <p> 860 Bn cng nn thanh lc thng tin u vo ca ngi dng khi gi 861 {@link android.content.ContentResolver#delete ContentResolver.delete()}. tm hiu thm v 862 iu ny, hy c phn <a href="#Injection">Bo v trc mc nhp c hi</a>. 863 </p> 864 <!-- Provider Data Types --> 865 <h2 id="DataTypes">Cc Kiu D liu ca Trnh cung cp</h2> 866 <p> 867 Trnh cung cp ni dung c th cung cp nhiu kiu d liu khc nhau. Trnh cung cp T in Ngi dng ch cung cp 868 vn bn, nhng trnh cung cp cng c th cung cp cc nh dng sau: 869 </p> 870 <ul> 871 <li> 872 integer 873 </li> 874 <li> 875 long integer (long) 876 </li> 877 <li> 878 floating point 879 </li> 880 <li> 881 long floating point (double) 882 </li> 883 </ul> 884 <p> 885 Mt kiu d liu khc m cc trnh cung cp thng s dng l Binary Large OBject (BLOB) c trin khai nh mt 886 mng 64KB byte. Bn c th xem cc kiu d liu c sn bng cch xem cc phng php "get" lp 887 {@link android.database.Cursor}. 888 </p> 889 <p> 890 Kiu d liu i vi mi ct trong mt trnh cung cp thng c lit k trong ti liu ca trnh cung cp . 891 Cc kiu d liu dnh cho Trnh cung cp T in Ngi dng c lit k trong ti liu tham kho 892 cho lp hp ng {@link android.provider.UserDictionary.Words} ca n (lp hp ng c 893 m t trong phn <a href="#ContractClasses">Cc Lp Hp ng</a>). 894 Bn cng c th xc nh kiu d liu bng cch gi {@link android.database.Cursor#getType 895 Cursor.getType()}. 896 </p> 897 <p> 898 Trnh cung cp cng duy tr thng tin v kiu d liu MIME cho mi URI ni dung m chng nh ngha. Bn c th 899 s dng thng tin v kiu MIME tm hiu xem ng dng ca mnh c th x l d liu m 900 trnh cung cp a ra hay khng, hoc chn mt kiu x l da trn kiu MIME. Bn thng cn kiu 901 MIME khi ang lm vic vi mt trnh cung cp cha cc cu trc hoc tp 902 d liu phc tp. V d, bng {@link android.provider.ContactsContract.Data} 903 trong Trnh cung cp Danh b s dng cc kiu MIME dn nhn kiu d liu lin lc c lu tr trong tng 904 hng. nhn c kiu MIME tng ng vi mt URI ni dung, hy gi 905 {@link android.content.ContentResolver#getType ContentResolver.getType()}. 906 </p> 907 <p> 908 Phn <a href="#MIMETypeReference">Tham kho Kiu MIME</a> m t 909 c php ca c kiu MIME tiu chun ln ty chnh. 910 </p> 911 912 913 <!-- Alternative Forms of Provider Access --> 914 <h2 id="AltForms">Cc Hnh thc Truy cp Trnh cung cp Thay th</h2> 915 <p> 916 C ba hnh thc truy cp trnh cung cp thay th quan trng trong pht trin ng dng: 917 </p> 918 <ul> 919 <li> 920 <a href="#Batch">Truy cp hng lot</a>: Bn c th to mt lot lnh gi truy cp bng cc phng php trong 921 lp {@link android.content.ContentProviderOperation}, ri sau p dng chng vi 922 {@link android.content.ContentResolver#applyBatch ContentResolver.applyBatch()}. 923 </li> 924 <li> 925 Truy vn khng ng b: Bn nn thc hin cc truy vn trong mt lung ring. Mt cch lm iu ny l 926 s dng mt i tng {@link android.content.CursorLoader}. Cc v d trong 927 hng dn <a href="{@docRoot}guide/components/loaders.html">Trnh ti</a> s minh ha 928 cch lm iu ny. 929 </li> 930 <li> 931 <a href="#Intents">Truy cp d liu thng qua nh</a>: Mc d khng th gi mt nh 932 trc tip ti mt trnh cung cp, bn c th gi mt nh ti ng dng ca trnh cung cp , 933 y thng l cch tt nht sa i d liu ca trnh cung cp. 934 </li> 935 </ul> 936 <p> 937 Truy cp hng lot v sa i thng qua nh c m t trong cc phn sau. 938 </p> 939 <h3 id="Batch">Truy cp hng lot</h3> 940 <p> 941 Truy cp hng lot vo mt trnh cung cp l cch hu ch chn nhiu hng, hoc chn 942 cc hng vo nhiu bng trong cng lnh gi phng php, hoc nhn chung thc hin mt tp hp 943 thao tc qua cc ranh gii tin trnh nh mt giao tc (thao tc nguyn t). 944 </p> 945 <p> 946 truy cp mt trnh cung cp trong "ch hng lot", 947 bn to mt mng i tng {@link android.content.ContentProviderOperation} ri 948 phn phi chng ti mt trnh cung cp ni dung bng 949 {@link android.content.ContentResolver#applyBatch ContentResolver.applyBatch()}. Bn chuyn 950 <em>quyn</em> ca trnh cung cp ni dung cho phng php ny thay v mt URI ni dung c th. 951 iu ny cho php i tng {@link android.content.ContentProviderOperation} trong mng c tc dng 952 i vi mt bng khc. Mt lnh gi ti {@link android.content.ContentResolver#applyBatch 953 ContentResolver.applyBatch()} tr v mt mng kt qu. 954 </p> 955 <p> 956 M t lp hp ng {@link android.provider.ContactsContract.RawContacts} 957 bao gm mt on m HTML th hin vic chn hng lot. ng dng mu 958 <a href="{@docRoot}resources/samples/ContactManager/index.html">Trnh qun l Danh b</a> 959 c mt v d v truy cp hng lot trong tp ngun <code>ContactAdder.java</code> 960 ca n. 961 </p> 962 <div class="sidebox-wrapper"> 963 <div class="sidebox"> 964 <h2>Hin th d liu bng cch s dng mt ng dng trnh tr gip.</h2> 965 <p> 966 Nu ng dng ca bn <em>c</em> quyn truy cp, bn vn c th cn s dng mt 967 nh hin th d liu trong mt ng dng khc. V d, ng dng Lch chp nhn mt 968 nh {@link android.content.Intent#ACTION_VIEW}, nhm hin th mt ngy hoc s kin c th. 969 iu ny cho php bn hin th thng tin lch m khng phi to UI ca chnh mnh. 970 tm hiu thm v tnh nng ny, hy xem 971 hng dn <a href="{@docRoot}guide/topics/providers/calendar-provider.html">Trnh cung cp Lch</a>. 972 </p> 973 <p> 974 ng dng m bn gi nh n khng nht thit phi l ng dng 975 c lin kt vi trnh cung cp. V d, bn c th truy xut mt lin lc t 976 Trnh cung cp Danh b, ri gi mt nh {@link android.content.Intent#ACTION_VIEW} 977 cha URI ni dung cho hnh nh lin lc ti mt trnh xem nh. 978 </p> 979 </div> 980 </div> 981 <h3 id="Intents">Truy cp d liu thng qua nh</h3> 982 <p> 983 nh c th cho php truy cp gin tip vo mt trnh cung cp ni dung. Bn cho php ngi dng truy cp 984 d liu trong mt trnh cung cp ngay c khi ng dng ca bn khng c quyn truy cp, hoc bng cch 985 nhn li mt nh kt qu t mt ng dng c quyn, hoc bng cch kch hot mt 986 ng dng c php v cho php ngi dng c lm vic trong n. 987 </p> 988 <h4>c truy cp vi cc quyn tm thi</h4> 989 <p> 990 Bn c th truy cp d liu trong mt trnh cung cp ni dung, ngay c khi bn khng c quyn 991 truy nhp ph hp, bng cch gi mt nh ti mt ng dng c quyn v 992 nhn li mt nh kt qu cha quyn "URI". 993 y l nhng quyn cho mt URI ni dung c th ko di ti khi hot ng nhn chng 994 c hon thnh. ng dng c quyn lu di s cp quyn tm thi 995 bng cch t mt c trong nh kt qu: 996 </p> 997 <ul> 998 <li> 999 <strong>Quyn c:</strong> 1000 {@link android.content.Intent#FLAG_GRANT_READ_URI_PERMISSION} 1001 </li> 1002 <li> 1003 <strong>Quyn ghi:</strong> 1004 {@link android.content.Intent#FLAG_GRANT_WRITE_URI_PERMISSION} 1005 </li> 1006 </ul> 1007 <p class="note"> 1008 <strong>Lu :</strong> Nhng c ny khng cp quyn truy cp c v ghi ni chung cho trnh cung cp 1009 m c quyn c cha trong URI ni dung. Quyn truy cp ny ch p dng cho chnh URI . 1010 </p> 1011 <p> 1012 Trnh cung cp s nh ngha quyn URI cho cc URI ni dung trong bn k khai ca n, bng cch s dng thuc tnh 1013 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html#gprmsn">android:grantUriPermission</a></code> 1014 ca phn t 1015 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html"><provider></a></code> 1016 cng nh phn t con 1017 <code><a href="{@docRoot}guide/topics/manifest/grant-uri-permission-element.html"><grant-uri-permission></a></code> 1018 ca phn t 1019 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html"><provider></a></code> 1020 . C ch cp quyn URI ny c gii thch chi tit hn trong 1021 hng dn <a href="{@docRoot}guide/topics/security/security.html">Bo mt v Quyn</a>, 1022 trong phn "Quyn URI". 1023 </p> 1024 <p> 1025 V d, bn c th truy xut d liu cho mt lin lc trong Trnh cung cp Danh b, ngay c khi bn khng 1026 c quyn {@link android.Manifest.permission#READ_CONTACTS}. Bn c th mun thc hin iu ny 1027 trong mt ng dng gi thip mng in t ti mt lin lc vo ngy sinh nht ca ngi . Thay v 1028 yu cu {@link android.Manifest.permission#READ_CONTACTS}, l ni cp cho bn quyn truy cp tt c lin lc 1029 ca ngi dng v tt c thng tin ca h, bn nn cho php ngi dng kim sot nhng lin lc 1030 no c s dng bi ng dng ca bn. lm iu ny, bn s dng tin trnh sau: 1031 </p> 1032 <ol> 1033 <li> 1034 ng dng ca bn gi mt nh cha hnh ng 1035 {@link android.content.Intent#ACTION_PICK} v kiu MIME "danh b" 1036 {@link android.provider.ContactsContract.RawContacts#CONTENT_ITEM_TYPE}, bng cch s dng 1037 phng php {@link android.app.Activity#startActivityForResult 1038 startActivityForResult()}. 1039 </li> 1040 <li> 1041 V nh ny khp vi b lc nh cho hot ng 1042 "la chn" ca ng dng Danh b, hot ng s i n tin cnh. 1043 </li> 1044 <li> 1045 Trong hot ng la chn, ngi dng chn mt 1046 lin lc cp nht. Khi iu ny xy ra, hot ng la chn s gi 1047 {@link android.app.Activity#setResult setResult(resultcode, intent)} 1048 thit lp mt nh nhm gi li ng dng ca bn. nh cha URI ni dung 1049 ca lin lc m ngi dng chn, v cc c "ph thm" 1050 {@link android.content.Intent#FLAG_GRANT_READ_URI_PERMISSION}. Nhng c ny cp quyn URI 1051 cho ng dng ca bn c d liu cho lin lc c tr n bi 1052 URI ni dung. Sau , hot ng la chn gi {@link android.app.Activity#finish()} 1053 tr kim sot v ng dng ca bn. 1054 </li> 1055 <li> 1056 Hot ng ca bn tr v tin cnh, v h thng s gi phng php 1057 {@link android.app.Activity#onActivityResult onActivityResult()} 1058 ca hot ng ca bn. Phng php ny nhn c nh kt qu do hot ng la chn to trong 1059 ng dng Danh b. 1060 </li> 1061 <li> 1062 Vi URI ni dung t nh kt qu, bn c th c d liu ca lin lc 1063 t Trnh cung cp Danh b, ngay c khi bn khng yu cu quyn truy cp c lu di 1064 vo trnh cung cp trong bn k khai ca mnh. Sau , bn c th nhn thng tin ngy sinh ca lin lc 1065 hoc a ch e-mail ca ngi ri gi thip mng in t. 1066 </li> 1067 </ol> 1068 <h4>S dng mt ng dng khc</h4> 1069 <p> 1070 Mt cch n gin cho php ngi dng sa i d liu m bn khng c quyn truy cp l 1071 kch hot mt ng dng c quyn v cho php ngi dng lm vic . 1072 </p> 1073 <p> 1074 V d, ng dng Lch chp nhn mt 1075 nh {@link android.content.Intent#ACTION_INSERT}, n cho php bn kch hot UI chn 1076 ca ng dng. Bn c th chuyn d liu "ph thm" trong nh ny m c ng dng s dng 1077 in trc vo UI. V cc s kin nh k c c php phc tp, cch 1078 u tin chn s kin vo Trnh cung cp Lch l kch hot ng dng Lch vi mt 1079 {@link android.content.Intent#ACTION_INSERT} ri ngi dng chn s kin ti . 1080 </p> 1081 <!-- Contract Classes --> 1082 <h2 id="ContractClasses">Cc Lp Hp ng</h2> 1083 <p> 1084 Lp hp ng nh ngha cc hng s s gip ng dng hot ng vi cc URI ni dung, tn 1085 ct, hnh ng nh, v cc tnh nng khc ca mt trnh cung cp ni dung. Cc lp hp ng khng 1086 c t ng a vo cng mt trnh cung cp; nh pht trin ca trnh cung cp phi nh ngha chng ri 1087 cung cp chng cho cc nh pht trin khc. Nhiu trnh cung cp c bao gm cng vi nn tng 1088 Android c cc lp hp ng tng ng trong gi {@link android.provider}. 1089 </p> 1090 <p> 1091 V d, Trnh cung cp T in Ngi dng c mt lp hp ng 1092 {@link android.provider.UserDictionary} cha cc hng s URI ni dung v tn ct. URI ni dung 1093 i vi bng "t" c nh ngha trong hng s 1094 {@link android.provider.UserDictionary.Words#CONTENT_URI UserDictionary.Words.CONTENT_URI}. 1095 Lp {@link android.provider.UserDictionary.Words} cng cha cc hng s tn ct, 1096 chng c s dng trong on m HTML mu trong hng dn ny. V d, mt d tho truy vn c th c 1097 nh ngha l: 1098 </p> 1099 <pre> 1100 String[] mProjection = 1101 { 1102 UserDictionary.Words._ID, 1103 UserDictionary.Words.WORD, 1104 UserDictionary.Words.LOCALE 1105 }; 1106 </pre> 1107 <p> 1108 Mt lp hp ng khc l {@link android.provider.ContactsContract} dnh cho Trnh cung cp Danh b. 1109 Ti liu tham kho cho lp ny bao gm cc on m HTML mu. Mt trong s cc 1110 lp con ca n, {@link android.provider.ContactsContract.Intents.Insert}, l mt lp hp ng 1111 cha cc hng s cho nh v d liu nh. 1112 </p> 1113 1114 1115 <!-- MIME Type Reference --> 1116 <h2 id="MIMETypeReference">Tham kho Kiu MIME</h2> 1117 <p> 1118 Trnh cung cp ni dung c th tr v cc kiu phng tin MIME tiu chun, hoc xu kiu MIME ty chnh, hoc c hai. 1119 </p> 1120 <p> 1121 Cc kiu MIME c nh dng 1122 </p> 1123 <pre> 1124 <em>type</em>/<em>subtype</em> 1125 </pre> 1126 <p> 1127 V d, kiu MIME thng dng <code>text/html</code> c kiu <code>text</code> v 1128 kiu con <code>html</code>. Nu trnh cung cp tr v loi ny cho mt URI, iu c ngha rng mt 1129 truy vn ang s dng URI s tr v vn bn cha th HTML. 1130 </p> 1131 <p> 1132 Cc xu kiu MIME ty chnh, cn gi l kiu MIME "theo nh cung cp", c cc gi tr 1133 <em>kiu</em> v <em>kiu con</em> phc tp hn. Gi tr <em>kiu</em> lun lun 1134 </p> 1135 <pre> 1136 vnd.android.cursor.<strong>dir</strong> 1137 </pre> 1138 <p> 1139 p dng cho nhiu hng, hoc 1140 </p> 1141 <pre> 1142 vnd.android.cursor.<strong>item</strong> 1143 </pre> 1144 <p> 1145 p dng cho mt hng. 1146 </p> 1147 <p> 1148 Gi tr <em>kiu con</em> p dng theo trnh cung cp. Cc trnh cung cp c tch hp trong Android thng c mt kiu con 1149 n gin. V d, khi ng dng Danh b to mt hng cho mt s in thoi, 1150 n t kiu MIME sau trong hng: 1151 </p> 1152 <pre> 1153 vnd.android.cursor.item/phone_v2 1154 </pre> 1155 <p> 1156 rng gi tr kiu con n gin l <code>phone_v2</code>. 1157 </p> 1158 <p> 1159 Cc nh pht trin trnh cung cp khc c th to mu hnh kiu con ca ring mnh da trn quyn 1160 v tn bng ca trnh cung cp. V d, xt mt trnh cung cp cha cc biu thi gian lch tu. 1161 Quyn ca trnh cung cp l <code>com.example.trains</code>, v n cha cc bng 1162 Line1, Line2, v Line3. phn hi li URI ni dung 1163 </p> 1164 <p> 1165 <pre> 1166 content://com.example.trains/Line1 1167 </pre> 1168 <p> 1169 i vi bng Line1, trnh cung cp tr v kiu MIME 1170 </p> 1171 <pre> 1172 vnd.android.cursor.<strong>dir</strong>/vnd.example.line1 1173 </pre> 1174 <p> 1175 phn hi li URI ni dung 1176 </p> 1177 <pre> 1178 content://com.example.trains/Line2/5 1179 </pre> 1180 <p> 1181 i vi hng 5 trong bng Line2, trnh cung cp tr v kiu MIME 1182 </p> 1183 <pre> 1184 vnd.android.cursor.<strong>item</strong>/vnd.example.line2 1185 </pre> 1186 <p> 1187 Hu ht cc trnh cung cp ni dung u nh ngha hng s lp hp ng cho cc kiu MIME m chng s dng. V d nh lp hp ng 1188 ca Trnh cung cp Danh b {@link android.provider.ContactsContract.RawContacts}, 1189 s nh ngha hng s 1190 {@link android.provider.ContactsContract.RawContacts#CONTENT_ITEM_TYPE} cho kiu MIME ca 1191 mt hng lin lc th duy nht. 1192 </p> 1193 <p> 1194 Cc URI ni dung i vi hng duy nht c m t trong phn 1195 <a href="#ContentURIs">URI ni dung</a>. 1196 </p> 1197