1 page.title=To mt Trnh cung cp Ni dung 2 @jd:body 3 <div id="qv-wrapper"> 4 <div id="qv"> 5 6 7 <h2>Trong ti liu ny</h2> 8 <ol> 9 <li> 10 <a href="#DataStorage">Thit k Kho lu tr D liu</a> 11 </li> 12 <li> 13 <a href="#ContentURI">Thit k URI Ni dung</a> 14 </li> 15 <li> 16 <a href="#ContentProvider">Trin khai Lp Trnh cung cp Ni dung</a> 17 <ol> 18 <li> 19 <a href="#RequiredAccess">Phng php c Yu cu</a> 20 </li> 21 <li> 22 <a href="#Query">Trin khai phng php query()</a> 23 </li> 24 <li> 25 <a href="#Insert">Trin khai phng php insert()</a> 26 </li> 27 <li> 28 <a href="#Delete">Trin khai phng php delete()</a> 29 </li> 30 <li> 31 <a href="#Update">Trin khai phng php update()</a> 32 </li> 33 <li> 34 <a href="#OnCreate">Trin khai phng php onCreate()</a> 35 </li> 36 </ol> 37 </li> 38 <li> 39 <a href="#MIMETypes">Trin khai Kiu MIME ca Trnh cung cp Ni dung</a> 40 <ol> 41 <li> 42 <a href="#TableMIMETypes">Kiu MIME cho bng</a> 43 </li> 44 <li> 45 <a href="#FileMIMETypes">Kiu MIME cho tp</a> 46 </li> 47 </ol> 48 </li> 49 <li> 50 <a href="#ContractClass">Trin khai mt Lp Hp ng</a> 51 </li> 52 <li> 53 <a href="#Permissions">Trin khai Quyn ca Trnh cung cp Ni dung</a> 54 </li> 55 <li> 56 <a href="#ProviderElement">Phn t <provider></a> 57 </li> 58 <li> 59 <a href="#Intents"> nh v Truy cp D liu</a> 60 </li> 61 </ol> 62 <h2>Lp kha</h2> 63 <ol> 64 <li> 65 {@link android.content.ContentProvider} 66 </li> 67 <li> 68 {@link android.database.Cursor} 69 </li> 70 <li> 71 {@link android.net.Uri} 72 </li> 73 </ol> 74 <h2>Cc Mu Lin quan</h2> 75 <ol> 76 <li> 77 <a href="{@docRoot}resources/samples/NotePad/index.html"> 78 ng dng mu Note Pad 79 </a> 80 </li> 81 </ol> 82 <h2>Xem thm</h2> 83 <ol> 84 <li> 85 <a href="{@docRoot}guide/topics/providers/content-provider-basics.html"> 86 Ni dung C bn v Trnh cung cp Ni dung</a> 87 </li> 88 <li> 89 <a href="{@docRoot}guide/topics/providers/calendar-provider.html"> 90 Trnh cung cp Lch</a> 91 </li> 92 </ol> 93 </div> 94 </div> 95 96 97 <p> 98 Trnh cung cp ni dung qun l truy cp vo mt kho d liu tp trung. Bn trin khai mt 99 trnh cung cp thnh mt hoc nhiu lp trong mt ng dng Android, bn cnh cc phn t trong 100 tp bn k khai. Mt trong cc lp ca bn trin khai mt lp con 101 {@link android.content.ContentProvider}, y l giao din gia trnh cung cp ca bn v 102 cc ng dng khc. Mc d mc ch ca cc trnh cung cp ni dung khc l cung cp d liu c sn cho cc 103 ng dng khc, d nhin bn c th ra lnh cho cc hot ng trong ng dng ca mnh 104 truy vn v sa i d liu c qun l bi trnh cung cp ca bn. 105 </p> 106 <p> 107 Phn cn li ca ch ny l mt danh sch c bn v cc bc xy dng mt trnh cung cp ni dung v mt danh sch 108 cc API s dng. 109 </p> 110 111 112 <!-- Before You Start Building --> 113 <h2 id="BeforeYouStart">Trc khi Bn Bt u Xy dng</h2> 114 <p> 115 Trc khi bn bt u xy dng mt trnh cung cp, hy lm vic sau: 116 </p> 117 <ol> 118 <li> 119 <strong>Quyt nh xem bn c cn mt trnh cung cp ni dung khng</strong>. Bn cn xy dng mt trnh cung cp 120 ni dung nu mun cung cp mt hoc nhiu tnh nng sau y: 121 <ul> 122 <li>Bn mun cung cp d liu hoc tp phc tp cho cc ng dng khc.</li> 123 <li>Bn mun cho php ngi dng sao chp d liu phc tp t ng dng ca bn vo cc ng dng khc.</li> 124 <li>Bn mun cung cp cc gi tm kim ty chnh bng cch s dng khun kh tm kim.</li> 125 </ul> 126 <p> 127 Bn <em>khng</em> cn trnh cung cp phi s dng mt c s d liu SQLite nu vic s dng hon ton 128 din ra trong ng dng ca bn. 129 </p> 130 </li> 131 <li> 132 Nu bn cha lm nh vy, hy c ch 133 <a href="{@docRoot}guide/topics/providers/content-provider-basics.html"> 134 Ni dung C bn v Trnh cung cp Ni dung</a> tm hiu thm v trnh cung cp. 135 </li> 136 </ol> 137 <p> 138 Tip theo, hy lm theo nhng bc sau xy dng trnh cung cp ca bn: 139 </p> 140 <ol> 141 <li> 142 Thit k kho lu tr th cho d liu ca bn. Mt trnh cung cp ni dung s cung cp d liu theo hai cch: 143 <dl> 144 <dt> 145 D liu tp 146 </dt> 147 <dd> 148 D liu m thng n cc tp chng hn nh 149 nh, m thanh, hoc video. Lu tr cc tp khng gian 150 ring t trong ng dng ca bn. hi p li mt yu cu tp t mt ng dng khc, trnh cung cp 151 ca bn c th cung cp mt nm iu tc cho tp. 152 </dd> 153 <dt> 154 D liu "cu trc" 155 </dt> 156 <dd> 157 D liu m thng n mt c s d liu, mng, hoc cu trc tng t. 158 Lu tr d liu di dng tng thch vi cc bng hng ct. Hng 159 biu din mt i tng, chng hn nh mt ngi hoc khon mc trong kim k. Ct biu din 160 mt s d liu cho i tng, chng hn nh tn ca mt ngi hoc gi ca mt khon mc. Mt cch thng dng 161 lu tr loi d liu ny l trong c s d liu SQLite, nhng bn c th s dng bt k loi 162 kho lu tr lu di no. tm hiu thm v cc loi kho lu tr c sn trong 163 h thng Android, hy xem phn <a href="#DataStorage"> 164 Thit k Kho lu tr D liu</a>. 165 </dd> 166 </dl> 167 </li> 168 <li> 169 nh ngha mt trin khai c th ca lp {@link android.content.ContentProvider} v 170 cc phng php c yu cu ca n. Lp ny l giao din gia d liu ca bn v phn cn li ca 171 h thng Android. bit thm thng tin v lp ny, hy xem phn 172 <a href="#ContentProvider">Trin khai Lp ContentProvider</a>. 173 </li> 174 <li> 175 nh ngha xu thm quyn ca trnh cung cp, URI ni dung ca n, v cc tn ct. Nu bn mun 176 ng dng ca trnh cung cp x l cc nh, hy nh ngha cc hnh ng nh, d liu ph thm, 177 v c. ng thi, hy nh ngha cc quyn m bn s yu cu cho nhng ng dng mun 178 truy cp d liu ca bn. Bn nn cn nhc nh ngha tt c nhng gi tr ny l hng s trong mt 179 lp ring; sau , bn c th cho hin lp ny ra vi cc nh pht trin khc. bit thm 180 thng tin v URI ni dung, hy xem 181 phn <a href="#ContentURI">Thit k URI Ni dung</a>. 182 bit thm thng tin v nh, hy xem 183 phn <a href="#Intents"> nh v Truy cp D liu</a>. 184 </li> 185 <li> 186 Thm cc ni dung ty chn khc, chng hn nh d liu mu hoc trin 187 khai {@link android.content.AbstractThreadedSyncAdapter} m c th ng b ho d liu gia 188 trnh cung cp v d liu nn m my. 189 </li> 190 </ol> 191 192 193 <!-- Designing Data Storage --> 194 <h2 id="DataStorage">Thit k Kho lu tr D liu</h2> 195 <p> 196 Trnh cung cp ni dung l giao din i vi d liu c lu theo mt nh dng cu trc. Trc khi to 197 giao din, bn phi quyt nh cch lu tr d liu. Bn c th lu tr d liu theo bt k dng no 198 m bn mun ri thit k giao din c v ghi d liu nu cn thit. 199 </p> 200 <p> 201 C mt s cng ngh lu tr d liu c sn trong Android: 202 </p> 203 <ul> 204 <li> 205 H thng Android bao gm mt API c s d liu SQLite m cc trnh cung cp ca chnh Androi s dng 206 lu tr d liu theo nh hng bng. Lp 207 {@link android.database.sqlite.SQLiteOpenHelper} gip bn to c s d liu, v lp 208 {@link android.database.sqlite.SQLiteDatabase} l lp c bn nh gi 209 cc c s d liu. 210 <p> 211 Nh rng bn khng phi s dng mt c s d liu trin khai kho lu gi ca mnh. B ngoi, mt trnh cung cp 212 c dng nh l mt tp hp bng, tng t nh mt c s d liu quan h, nhng y 213 khng phi l mt yu cu i vi vic trin khai ni b ca trnh cung cp. 214 </p> 215 </li> 216 <li> 217 lu tr d liu tp, Android c nhiu API nh hng tp khc nhau. 218 tm hiu thm v lu tr tp, hy c ch 219 <a href="{@docRoot}guide/topics/data/data-storage.html">Kho lu tr D liu</a>. Nu bn 220 ang thit k mt trnh cung cp d liu lin quan ti phng tin chng hn nh nhc hay video, bn c th 221 c mt trnh cung cp cho php kt hp d liu bng v cc tp. 222 </li> 223 <li> 224 lm vic vi d liu trn nn mng, hy s dng cc lp trong {@link java.net} v 225 {@link android.net}. Bn cng c th ng b ho d liu trn nn mng vi mt kho lu tr d liu cc b 226 chng hn nh mt c s d liu, ri cung cp d liu di dng bng hoc tp. 227 ng dng mu <a href="{@docRoot}resources/samples/SampleSyncAdapter/index.html"> 228 Trnh iu hp ng b Mu</a> minh ha loi ng b ho ny. 229 </li> 230 </ul> 231 <h3 id="DataDesign"> 232 Nhng ni dung cn xem xt khi thit k d liu 233 </h3> 234 <p> 235 Sau y l mt s mo thit k cu trc d liu cho trnh cung cp ca bn: 236 </p> 237 <ul> 238 <li> 239 D liu bng nn lun c mt ct "kha chnh" m trnh cung cp duy tr 240 nh mt gi tr s duy nht cho mi hng. Bn c th s dng gi tr ny lin kt hng vi cc hng 241 c lin quan trong cc bng khc (s dng n lm "kha ngoi"). Mc d bn c th s dng bt k tn gi no 242 cho ct ny, s dng {@link android.provider.BaseColumns#_ID BaseColumns._ID} l la chn tt nht 243 v vic lin kt cc kt qu ca mt truy vn trnh cung cp vi 244 {@link android.widget.ListView} i hi mt trong cc ct c truy xut phi c tn 245 <code>_ID</code>. 246 </li> 247 <li> 248 Nu bn mun cung cp cc hnh nh bitmap hoc ni dung d liu nh hng tp rt ln khc, hy lu tr 249 d liu vo mt tp ri cung cp n gin tip thay v lu tr n trc tip trong mt 250 bng. Nu lm vy, bn cn bo cho ngi dng trnh cung cp ca bn rng h cn s dng mt phng php tp 251 {@link android.content.ContentResolver} truy cp d liu. 252 </li> 253 <li> 254 S dng kiu d liu Binary Large OBject (BLOB) lu tr d liu c kch c khc nhau hoc c mt 255 cu trc thay i. V d, bn c th s dng ct BLOB lu tr mt 256 <a href="http://code.google.com/p/protobuf">b m giao thc</a> hay 257 <a href="http://www.json.org">cu trc JSON</a>. 258 <p> 259 Bn cng c th s dng mt BLOB trin khai mt bng <em>c lp vi s </em>. Trong 260 kiu bng ny, bn nh ngha mt ct kha chnh, mt ct kiu MIME, v mt hoc 261 nhiu ct chung l BLOB. ngha ca d liu trong ct BLOB c th hin 262 bi gi tr trong ct kiu MIME. iu ny cho php bn lu tr cc kiu hng khc nhau trong 263 cng bng. Bng "d liu" 264 {@link android.provider.ContactsContract.Data} ca Trnh cung cp Danh b l mt v d v bng 265 c lp vi s . 266 </p> 267 </li> 268 </ul> 269 <!-- Designing Content URIs --> 270 <h2 id="ContentURI">Thit k URI Ni dung</h2> 271 <p> 272 <strong>URI ni dung</strong> l mt URI xc nh d liu trong mt trnh cung cp. URI ni dung bao gm 273 tn mang tnh biu tng ca ton b trnh cung cp (<strong>quyn</strong> ca n) v mt 274 tn tr n mt bng hoc tp (<strong>ng dn</strong>). Phn id ty chn ch n mt 275 hng ring l trong mt bng. Mi phng thc truy cp d liu 276 {@link android.content.ContentProvider} u c mt URI ni dung l mt tham i; iu ny cho php bn 277 xc nh bng, hng, hoc tp truy cp. 278 </p> 279 <p> 280 Ni dung c bn ca URI ni dung c m t trong ch 281 <a href="{@docRoot}guide/topics/providers/content-provider-basics.html"> 282 Ni dung C bn v Trnh cung cp Ni dung</a>. 283 </p> 284 <h3>Thit k mt thm quyn</h3> 285 <p> 286 Mt trnh cung cp thng c mt thm quyn duy nht, ng vai tr l tn ni b Android ca n. 287 trnh xung t vi cc trnh cung cp khc, bn nn s dng quyn s hu min Internet (o ngc) 288 lm c s cho thm quyn ca trnh cung cp ca mnh. V xut ny cng ng i vi tn gi 289 Android, bn c th nh ngha thm quyn trnh cung cp ca mnh l phn m rng ca tn 290 gi cha trnh cung cp. V d, nu tn gi Android l 291 <code>com.example.<appname></code>, bn nn cp cho trnh cung cp ca mnh 292 thm quyn <code>com.example.<appname>.provider</code>. 293 </p> 294 <h3>Thit k mt cu trc ng dn</h3> 295 <p> 296 Nh pht trin thng to URI ni dung t thm quyn bng cch ni cc ng dn tr n 297 cc bng ring l. V d, nu bn c hai bng <em>table1</em> v 298 <em>table2</em>, bn kt hp thm quyn t v d trc to ra 299 cc URI ni dung 300 <code>com.example.<appname>.provider/table1</code> v 301 <code>com.example.<appname>.provider/table2</code>. Cc ng dn 302 khng b gii hn mt phn on duy nht, v khng cn phi c mt bng cho tng cp ca ng dn. 303 </p> 304 <h3>X l ID URI ni dung</h3> 305 <p> 306 Theo quy c, cc trnh cung cp cho php truy cp mt hng n trong mt bng bng cch chp nhn mt URI ni dung 307 c mt gi tr ID cho hng cui URI. Cng theo quy c, cc trnh cung cp s so khp 308 gi tr ID vi ct <code>_ID</code> ca bng, v thc hin truy cp yu cu i vi 309 hng trng khp. 310 </p> 311 <p> 312 Quy c ny to iu kin cho mt kiu mu thit k chung cho cc ng dng truy cp mt trnh cung cp. ng dng 313 tin hnh truy vn i vi trnh cung cp v hin th kt qu {@link android.database.Cursor} 314 trong mt {@link android.widget.ListView} bng cch s dng {@link android.widget.CursorAdapter}. 315 nh ngha {@link android.widget.CursorAdapter} yu cu mt trong cc ct trong 316 {@link android.database.Cursor} phi l <code>_ID</code> 317 </p> 318 <p> 319 Sau , ngi dng chn mt trong cc hng c hin th t UI xem hoc sa i 320 d liu. ng dng s nhn c hng tng ng t {@link android.database.Cursor} lm nn cho 321 {@link android.widget.ListView}, nhn gi tr <code>_ID</code> cho hng ny, ni n vi 322 URI ni dung, v gi yu cu truy cp ti trnh cung cp. Sau , trnh cung cp c th thc hin 323 truy vn hoc sa i i vi chnh xc hng m ngi dng chn. 324 </p> 325 <h3>Kiu mu URI ni dung</h3> 326 <p> 327 gip bn chn hnh ng no s thc hin cho URI ni dung n, API ca trnh cung cp s bao gm 328 lp thun tin {@link android.content.UriMatcher}, n nh x "kiu mu" URI ni dung vi 329 cc gi tr s nguyn. Bn c th s dng cc gi tr s nguyn trong mt cu lnh <code>switch</code> m chn 330 hnh ng mong mun cho URI ni dung hoc URI m khp vi mt kiu mu c th. 331 </p> 332 <p> 333 Kiu mu URI ni dung s so khp cc URI ni dung bng cch s dng k t i din: 334 </p> 335 <ul> 336 <li> 337 <strong><code>*</code>:</strong> Khp mt xu k t hp l bt k vi chiu di bt k. 338 </li> 339 <li> 340 <strong><code>#</code>:</strong> Khp mt xu k t s c chiu di bt k. 341 </li> 342 </ul> 343 <p> 344 Ly mt v d v thit k v to m x l URI ni dung, hy xt mt trnh cung cp c 345 thm quyn <code>com.example.app.provider</code> m nhn ra cc URI ni dung 346 tr n cc bng sau: 347 </p> 348 <ul> 349 <li> 350 <code>content://com.example.app.provider/table1</code>: Mt bng gi l <code>table1</code>. 351 </li> 352 <li> 353 <code>content://com.example.app.provider/table2/dataset1</code>: Mt bng gi l 354 <code>dataset1</code>. 355 </li> 356 <li> 357 <code>content://com.example.app.provider/table2/dataset2</code>: Mt bng gi l 358 <code>dataset2</code>. 359 </li> 360 <li> 361 <code>content://com.example.app.provider/table3</code>: Mt bng gi l <code>table3</code>. 362 </li> 363 </ul> 364 <p> 365 Trnh cung cp cng nhn ra nhng URI ni dung ny nu chng c mt ID hng c ni km, nh 366 v d <code>content://com.example.app.provider/table3/1</code> i vi hng c nhn bit bi 367 <code>1</code> trong <code>table3</code>. 368 </p> 369 <p> 370 S c th c cc kiu mu URI ni dung sau: 371 </p> 372 <dl> 373 <dt> 374 <code>content://com.example.app.provider/*</code> 375 </dt> 376 <dd> 377 Khp vi bt k URI ni dung no trong trnh cung cp. 378 </dd> 379 <dt> 380 <code>content://com.example.app.provider/table2/*</code>: 381 </dt> 382 <dd> 383 Khp vi mt URI ni dung cho cc bng <code>dataset1</code> 384 v <code>dataset2</code>, nhng khng khp vi URI ni dung cho <code>table1</code> hoc 385 <code>table3</code>. 386 </dd> 387 <dt> 388 <code>content://com.example.app.provider/table3/#</code>: Khp vi mt URI ni dung 389 cho cc hng n trong <code>table3</code>, chng hn nh 390 <code>content://com.example.app.provider/table3/6</code> i vi hng c xc nh bi 391 <code>6</code>. 392 </dt> 393 </dl> 394 <p> 395 on m HTML sau cho bit cch hot ng ca cc phng php trong {@link android.content.UriMatcher}. 396 on m ny x l cc URI cho ton b mt bng khc vi URI cho mt 397 hng n, bng cch s dng mu hnh URI ni dung 398 <code>content://<authority>/<path></code> cho cc bng, v 399 <code>content://<authority>/<path>/<id></code> cho cc hng n. 400 </p> 401 <p> 402 Phng php {@link android.content.UriMatcher#addURI(String, String, int) addURI()} nh x mt 403 thm quyn v ng dn ti mt gi tr s nguyn. Phng php {@link android.content.UriMatcher#match(Uri) 404 match()} tr v gi tr s nguyn cho mt URI. Cu lnh <code>switch</code> s chn 405 gia truy vn ton b bng v truy vn cho mt bn ghi n: 406 </p> 407 <pre class="prettyprint"> 408 public class ExampleProvider extends ContentProvider { 409 ... 410 // Creates a UriMatcher object. 411 private static final UriMatcher sUriMatcher; 412 ... 413 /* 414 * The calls to addURI() go here, for all of the content URI patterns that the provider 415 * should recognize. For this snippet, only the calls for table 3 are shown. 416 */ 417 ... 418 /* 419 * Sets the integer value for multiple rows in table 3 to 1. Notice that no wildcard is used 420 * in the path 421 */ 422 sUriMatcher.addURI("com.example.app.provider", "table3", 1); 423 424 /* 425 * Sets the code for a single row to 2. In this case, the "#" wildcard is 426 * used. "content://com.example.app.provider/table3/3" matches, but 427 * "content://com.example.app.provider/table3 doesn't. 428 */ 429 sUriMatcher.addURI("com.example.app.provider", "table3/#", 2); 430 ... 431 // Implements ContentProvider.query() 432 public Cursor query( 433 Uri uri, 434 String[] projection, 435 String selection, 436 String[] selectionArgs, 437 String sortOrder) { 438 ... 439 /* 440 * Choose the table to query and a sort order based on the code returned for the incoming 441 * URI. Here, too, only the statements for table 3 are shown. 442 */ 443 switch (sUriMatcher.match(uri)) { 444 445 446 // If the incoming URI was for all of table3 447 case 1: 448 449 if (TextUtils.isEmpty(sortOrder)) sortOrder = "_ID ASC"; 450 break; 451 452 // If the incoming URI was for a single row 453 case 2: 454 455 /* 456 * Because this URI was for a single row, the _ID value part is 457 * present. Get the last path segment from the URI; this is the _ID value. 458 * Then, append the value to the WHERE clause for the query 459 */ 460 selection = selection + "_ID = " uri.getLastPathSegment(); 461 break; 462 463 default: 464 ... 465 // If the URI is not recognized, you should do some error handling here. 466 } 467 // call the code to actually do the query 468 } 469 </pre> 470 <p> 471 Mt lp khc, {@link android.content.ContentUris}, s cung cp cc phng php thun tin lm vic 472 vi phn <code>id</code> ca URI ni dung. Cc lp {@link android.net.Uri} v 473 {@link android.net.Uri.Builder} bao gm cc phng php thun tin cho vic phn tch cc i tng 474 {@link android.net.Uri} hin c v xy dng cc i tng mi. 475 </p> 476 477 <!-- Implementing the ContentProvider class --> 478 <h2 id="ContentProvider">Trin khai Lp Trnh cung cp Ni dung</h2> 479 <p> 480 Thc th {@link android.content.ContentProvider} qun l truy cp vo 481 mt tp d liu cu trc bng cch x l yu cu t cc ng dng khc. Tt c cc dng 482 truy cp cui cng u gi {@link android.content.ContentResolver}, sau n gi ra mt phng php 483 c th ca {@link android.content.ContentProvider} ly quyn truy cp. 484 </p> 485 <h3 id="RequiredAccess">Phng php c yu cu</h3> 486 <p> 487 Lp tm tt {@link android.content.ContentProvider} s nh ngha su phng php tm tt 488 m bn phi trin khai nh mt phn lp con c th ca mnh. Tt c nhng phng php ny ngoi tr 489 {@link android.content.ContentProvider#onCreate() onCreate()} u c gi ra bi mt ng dng my khch 490 ang c truy cp trnh cung cp ni dung ca bn: 491 </p> 492 <dl> 493 <dt> 494 {@link android.content.ContentProvider#query(Uri, String[], String, String[], String) 495 query()} 496 </dt> 497 <dd> 498 Truy xut d liu t trnh cung cp ca bn. S dng cc tham i chn bng 499 truy vn, cc hng v ct tr v, v th t sp xp ca kt qu. 500 Tr v d liu nh mt i tng {@link android.database.Cursor}. 501 </dd> 502 <dt> 503 {@link android.content.ContentProvider#insert(Uri, ContentValues) insert()} 504 </dt> 505 <dd> 506 Chn mt hng mi vo trnh cung cp ca bn. S dng cc tham i la chn 507 bng ch v nhn cc gi tr ct s dng. Tr v mt URI ni dung cho 508 hng mi chn. 509 </dd> 510 <dt> 511 {@link android.content.ContentProvider#update(Uri, ContentValues, String, String[]) 512 update()} 513 </dt> 514 <dd> 515 Cp nht cc hng hin ti trong trnh cung cp ca bn. S dng cc tham i la chn bng v hng 516 cp nht v nhn cc gi tr ct c cp nht. Tr v s hng c cp nht. 517 </dd> 518 <dt> 519 {@link android.content.ContentProvider#delete(Uri, String, String[]) delete()} 520 </dt> 521 <dd> 522 Xa hng khi trnh cung cp ca bn. S dng cc tham i la chn bng v cc hng 523 cn xa. Tr v s hng c xa. 524 </dd> 525 <dt> 526 {@link android.content.ContentProvider#getType(Uri) getType()} 527 </dt> 528 <dd> 529 Tr v kiu MIME tng ng vi mt URI ni dung. Phng php ny c m t chi tit hn 530 trong phn <a href="#MIMETypes">Trin khai Kiu MIME ca Trnh cung cp Ni dung</a>. 531 </dd> 532 <dt> 533 {@link android.content.ContentProvider#onCreate() onCreate()} 534 </dt> 535 <dd> 536 Khi to trnh cung cp ca bn. H thng Android s gi ra phng php ny ngay lp tc sau khi n 537 to trnh cung cp ca bn. rng trnh cung cp ca bn khng c to cho n khi i tng 538 {@link android.content.ContentResolver} c truy cp n. 539 </dd> 540 </dl> 541 <p> 542 rng nhng phng php ny c cng ch k nh cc phng php 543 {@link android.content.ContentResolver} c t tn nh nhau. 544 </p> 545 <p> 546 Vic bn trin khai nhng phng php ny nn xt ti cc ni dung sau: 547 </p> 548 <ul> 549 <li> 550 Tt c phng php ny ngoi tr {@link android.content.ContentProvider#onCreate() onCreate()} 551 u c th c gi ng thi bng nhiu lung, v th chng phi an ton i vi lung. tm hiu 552 thm v nhiu lung, hy xem ch 553 <a href="{@docRoot}guide/components/processes-and-threads.html"> 554 Tin trnh v Lung</a>. 555 </li> 556 <li> 557 Trnh thc hin nhng thao tc di trong {@link android.content.ContentProvider#onCreate() 558 onCreate()}. Hon cc tc v khi to ti khi chng thc s cn thit. 559 Phn <a href="#OnCreate">Trin khai phng php onCreate()</a> 560 s bn k hn v vn ny. 561 </li> 562 <li> 563 Mc d bn phi trin khai nhng phng php ny, m ca bn khng nht thit phi lm g ngoi tr vic 564 tr v kiu d liu k vng. V d, bn c th mun ngn nhng ng dng khc 565 chn d liu vo mt s bng. lm iu ny, bn c th b qua lnh gi ti 566 {@link android.content.ContentProvider#insert(Uri, ContentValues) insert()} v tr v 567 0. 568 </li> 569 </ul> 570 <h3 id="Query">Trin khai phng php query()</h3> 571 <p> 572 Phng php 573 {@link android.content.ContentProvider#query(Uri, String[], String, String[], String) 574 ContentProvider.query()} phi tr v mt i tng {@link android.database.Cursor}, nu khng n s tht bi 575 , a ra mt li {@link java.lang.Exception}. Nu bn ang s dng mt c s d liu SQLite lm kho lu tr d liu ca mnh 576 , bn c th ch cn tr v {@link android.database.Cursor} c tr v bi mt trong cc phng php 577 <code>query()</code> ca lp {@link android.database.sqlite.SQLiteDatabase}. 578 Nu truy vn khng khp vi bt k hng no, bn nn tr v mt thc th {@link android.database.Cursor} 579 c phng php {@link android.database.Cursor#getCount()} tr v 0. 580 Bn ch nn tr v <code>null</code> nu xy ra mt li ni b trong tin trnh truy vn. 581 </p> 582 <p> 583 Nu bn khng ang s dng mt c s d liu SQLite lm kho lu tr d liu ca mnh, hy s dng mt trong cc lp con c th 584 ca {@link android.database.Cursor}. V d, lp {@link android.database.MatrixCursor} s trin khai 585 mt con chy trong mi hng l mt mng ca {@link java.lang.Object}. Vi lp ny, 586 hy s dng {@link android.database.MatrixCursor#addRow(Object[]) addRow()} thm mt hng mi. 587 </p> 588 <p> 589 Nh rng h thng Android phi c th giao tip vi {@link java.lang.Exception} 590 qua cc ranh gii tin trnh. Android c th lm vy cho nhng trng hp ngoi l sau, iu ny c th hu ch 591 trong x l li truy vn: 592 </p> 593 <ul> 594 <li> 595 {@link java.lang.IllegalArgumentException} (Bn c th chn a ra li ny nu trnh cung cp ca bn 596 nhn mt URI ni dung khng hp l) 597 </li> 598 <li> 599 {@link java.lang.NullPointerException} 600 </li> 601 </ul> 602 <h3 id="Insert">Trin khai phng php insert()</h3> 603 <p> 604 Phng php {@link android.content.ContentProvider#insert(Uri, ContentValues) insert()} s thm mt 605 hng mi vo bng ph hp bng cch s dng cc gi tr trong tham i {@link android.content.ContentValues} 606 . Nu tn ct khng nm trong tham i {@link android.content.ContentValues}, bn c th 607 mun cung cp mt gi tr mc nh cho n hoc trong m trnh cung cp ca bn hoc trong s 608 c s d liu ca bn. 609 </p> 610 <p> 611 Phng php ny s tr v URI ni dung cho hng mi. xy dng iu ny, hy ni 612 gi tr <code>_ID</code> ca hng mi (hay kha chnh khc) vi URI ni dung ca bng bng cch s dng 613 {@link android.content.ContentUris#withAppendedId(Uri, long) withAppendedId()}. 614 </p> 615 <h3 id="Delete">Trin khai phng php delete()</h3> 616 <p> 617 Phng php {@link android.content.ContentProvider#delete(Uri, String, String[]) delete()} 618 khng cn phi xa hng thc cht khi kho lu tr d liu ca bn. Nu bn ang s dng mt trnh iu hp ng b 619 vi trnh cung cp ca mnh, bn nn cn nhc nh du mt hng xa 620 bng c "xa" thay v g b hng mt cch hon ton. Trnh iu hp ng b c th 621 kim tra cc hng xa v g b chng khi my ch trc khi xa chng khi trnh cung cp. 622 </p> 623 <h3 id="Update">Trin khai phng php update()</h3> 624 <p> 625 Phng php {@link android.content.ContentProvider#update(Uri, ContentValues, String, String[]) 626 update()} ly cng tham i {@link android.content.ContentValues} c s dng bi 627 {@link android.content.ContentProvider#insert(Uri, ContentValues) insert()}, v 628 cng tham i <code>selection</code> v <code>selectionArgs</code> c s dng bi 629 {@link android.content.ContentProvider#delete(Uri, String, String[]) delete()} v 630 {@link android.content.ContentProvider#query(Uri, String[], String, String[], String) 631 ContentProvider.query()}. iu ny c th cho php bn s dng li m gia nhng phng php ny. 632 </p> 633 <h3 id="OnCreate">Trin khai phng php onCreate()</h3> 634 <p> 635 H thng Android s gi {@link android.content.ContentProvider#onCreate() 636 onCreate()} khi n khi ng trnh cung cp. Bn ch nn thc hin cc tc v khi to chy nhanh 637 trong phng php ny, v hon vic to c s d liu v np d liu ti khi trnh cung cp thc s 638 nhn c yu cu cho d liu. Nu bn thc hin cc tc v di trong 639 {@link android.content.ContentProvider#onCreate() onCreate()}, bn s lm chm li 640 qu trnh khi ng ca trnh cung cp. n lt mnh, iu ny s lm chm hi p t trnh cung cp i vi cc 641 ng dng khc. 642 </p> 643 <p> 644 V d, nu bn ang s dng mt c s d liu SQLite, bn c th to 645 mt i tng {@link android.database.sqlite.SQLiteOpenHelper} mi trong 646 {@link android.content.ContentProvider#onCreate() ContentProvider.onCreate()}, 647 ri to cc bng SQL ln u tin khi bn m c s d liu. to iu kin cho iu ny, 648 ln u tin bn gi {@link android.database.sqlite.SQLiteOpenHelper#getWritableDatabase 649 getWritableDatabase()}, n s t ng gi ra phng php 650 {@link android.database.sqlite.SQLiteOpenHelper#onCreate(SQLiteDatabase) 651 SQLiteOpenHelper.onCreate()}. 652 </p> 653 <p> 654 Hai on m HTML sau minh ha tng tc gia 655 {@link android.content.ContentProvider#onCreate() ContentProvider.onCreate()} v 656 {@link android.database.sqlite.SQLiteOpenHelper#onCreate(SQLiteDatabase) 657 SQLiteOpenHelper.onCreate()}. on m HTML u tin l trin khai 658 {@link android.content.ContentProvider#onCreate() ContentProvider.onCreate()}: 659 </p> 660 <pre class="prettyprint"> 661 public class ExampleProvider extends ContentProvider 662 663 /* 664 * Defines a handle to the database helper object. The MainDatabaseHelper class is defined 665 * in a following snippet. 666 */ 667 private MainDatabaseHelper mOpenHelper; 668 669 // Defines the database name 670 private static final String DBNAME = "mydb"; 671 672 // Holds the database object 673 private SQLiteDatabase db; 674 675 public boolean onCreate() { 676 677 /* 678 * Creates a new helper object. This method always returns quickly. 679 * Notice that the database itself isn't created or opened 680 * until SQLiteOpenHelper.getWritableDatabase is called 681 */ 682 mOpenHelper = new MainDatabaseHelper( 683 getContext(), // the application context 684 DBNAME, // the name of the database) 685 null, // uses the default SQLite cursor 686 1 // the version number 687 ); 688 689 return true; 690 } 691 692 ... 693 694 // Implements the provider's insert method 695 public Cursor insert(Uri uri, ContentValues values) { 696 // Insert code here to determine which table to open, handle error-checking, and so forth 697 698 ... 699 700 /* 701 * Gets a writeable database. This will trigger its creation if it doesn't already exist. 702 * 703 */ 704 db = mOpenHelper.getWritableDatabase(); 705 } 706 } 707 </pre> 708 <p> 709 on m HTML tip theo l trin khai 710 {@link android.database.sqlite.SQLiteOpenHelper#onCreate(SQLiteDatabase) 711 SQLiteOpenHelper.onCreate()}, bao gm mt lp trnh tr gip: 712 </p> 713 <pre class="prettyprint"> 714 ... 715 // A string that defines the SQL statement for creating a table 716 private static final String SQL_CREATE_MAIN = "CREATE TABLE " + 717 "main " + // Table's name 718 "(" + // The columns in the table 719 " _ID INTEGER PRIMARY KEY, " + 720 " WORD TEXT" 721 " FREQUENCY INTEGER " + 722 " LOCALE TEXT )"; 723 ... 724 /** 725 * Helper class that actually creates and manages the provider's underlying data repository. 726 */ 727 protected static final class MainDatabaseHelper extends SQLiteOpenHelper { 728 729 /* 730 * Instantiates an open helper for the provider's SQLite data repository 731 * Do not do database creation and upgrade here. 732 */ 733 MainDatabaseHelper(Context context) { 734 super(context, DBNAME, null, 1); 735 } 736 737 /* 738 * Creates the data repository. This is called when the provider attempts to open the 739 * repository and SQLite reports that it doesn't exist. 740 */ 741 public void onCreate(SQLiteDatabase db) { 742 743 // Creates the main table 744 db.execSQL(SQL_CREATE_MAIN); 745 } 746 } 747 </pre> 748 749 750 <!-- Implementing ContentProvider MIME Types --> 751 <h2 id="MIMETypes">Trin khai Kiu MIME ca Trnh cung cp Ni dung</h2> 752 <p> 753 Lp {@link android.content.ContentProvider} c hai phng php tr v cc kiu MIME: 754 </p> 755 <dl> 756 <dt> 757 {@link android.content.ContentProvider#getType(Uri) getType()} 758 </dt> 759 <dd> 760 Mt trong cc phng php c yu cu m bn phi trin khai cho bt k trnh cung cp no. 761 </dd> 762 <dt> 763 {@link android.content.ContentProvider#getStreamTypes(Uri, String) getStreamTypes()} 764 </dt> 765 <dd> 766 Mt phng php m bn c d tnh s trin khai nu trnh cung cp ca bn cung cp tp. 767 </dd> 768 </dl> 769 <h3 id="TableMIMETypes">Kiu MIME cho bng</h3> 770 <p> 771 Phng php {@link android.content.ContentProvider#getType(Uri) getType()} tr v mt 772 {@link java.lang.String} theo nh dng MIME m m t kiu d liu c tr v bi tham i 773 URI ni dung. Tham i {@link android.net.Uri} c th l mt mu hnh thay v mt URI c th; 774 trong trng hp ny, bn nn tr v kiu d liu c lin kt vi cc URI ni dung m khp vi 775 mu hnh . 776 </p> 777 <p> 778 i vi cc kiu d liu ph bin nh vn bn, HTML, hay JPEG, 779 {@link android.content.ContentProvider#getType(Uri) getType()} s tr v 780 kiu MIME tiu chun cho d liu . Mt danh sch y v nhng kiu tiu chun ny c sn trn trang web 781 <a href="http://www.iana.org/assignments/media-types/index.htm">IANA MIME Media Types</a> 782 . 783 </p> 784 <p> 785 i vi cc URI ni dung m tr ti mt hng hoc cc hng ca bng d liu, 786 {@link android.content.ContentProvider#getType(Uri) getType()} s tr v 787 mt kiu MIME theo nh dng MIME ring cho nh cung cp ca Android: 788 </p> 789 <ul> 790 <li> 791 B phn kiu: <code>vnd</code> 792 </li> 793 <li> 794 B phn kiu con: 795 <ul> 796 <li> 797 Nu mu hnh URI p dng cho mt hng n: <code>android.cursor.<strong>item</strong>/</code> 798 </li> 799 <li> 800 Nu mu hnh URI p dng cho nhiu hn mt hng: <code>android.cursor.<strong>dir</strong>/</code> 801 </li> 802 </ul> 803 </li> 804 <li> 805 B phn ring theo trnh cung cp: <code>vnd.<name></code>.<code><type></code> 806 <p> 807 Bn cung cp <code><name></code> v <code><type></code>. 808 Gi tr <code><name></code> nn l gi tr duy nht ton cc, 809 v gi tr <code><type></code> nn l gi tr duy nht i vi mu hnh 810 URI tng ng. Mt la chn hay cho <code><name></code> l tn cng ty ca bn hoc 811 mt thnh phn no trong tn gi Android cho ng dng ca bn. Mt la chn hay cho 812 <code><type></code> l mt xu xc nh bng c lin kt vi 813 URI. 814 </p> 815 816 </li> 817 </ul> 818 <p> 819 V d, nu thm quyn ca mt trnh cung cp l 820 <code>com.example.app.provider</code>, v n lm hin ra mt bng c tn 821 <code>table1</code> th kiu MIME cho nhiu hng trong <code>table1</code> l: 822 </p> 823 <pre> 824 vnd.android.cursor.<strong>dir</strong>/vnd.com.example.provider.table1 825 </pre> 826 <p> 827 i vi mt hng n ca <code>table1</code>, kiu MIME l: 828 </p> 829 <pre> 830 vnd.android.cursor.<strong>item</strong>/vnd.com.example.provider.table1 831 </pre> 832 <h3 id="FileMIMETypes">Kiu MIME cho tp</h3> 833 <p> 834 Nu trnh cung cp ca bn cung cp tp, hy trin khai 835 {@link android.content.ContentProvider#getStreamTypes(Uri, String) getStreamTypes()}. 836 Phng php ny s tr v mt mng {@link java.lang.String} ca kiu MIME i vi cc tp m trnh cung cp ca bn 837 c th tr v cho mt URI ni dung cho trc. Bn nn lc cc kiu MIME m mnh cung cp bng tham i b lc 838 kiu MIME, sao cho bn ch tr v nhng kiu MIME m my khch mun x l. 839 </p> 840 <p> 841 V d, xt mt trnh cung cp hnh nh di dng tp c nh dng <code>.jpg</code>, 842 <code>.png</code> v <code>.gif</code>. 843 Nu mt ng dng gi {@link android.content.ContentResolver#getStreamTypes(Uri, String) 844 ContentResolver.getStreamTypes()} bng xu b lc <code>image/*</code> ( 845 m l mt "hnh nh"), 846 khi phng php {@link android.content.ContentProvider#getStreamTypes(Uri, String) 847 ContentProvider.getStreamTypes()} s tr v mng: 848 </p> 849 <pre> 850 { "image/jpeg", "image/png", "image/gif"} 851 </pre> 852 <p> 853 Nu ng dng ch quan tm n cc tp <code>.jpg</code>, vy n c th gi 854 {@link android.content.ContentResolver#getStreamTypes(Uri, String) 855 ContentResolver.getStreamTypes()} bng xu b lc <code>*\/jpeg</code>, v 856 {@link android.content.ContentProvider#getStreamTypes(Uri, String) 857 ContentProvider.getStreamTypes()} s tr v: 858 <pre> 859 {"image/jpeg"} 860 </pre> 861 <p> 862 Nu trnh cung cp ca bn khng cung cp bt k kiu MIME no c yu cu trong xu b lc, 863 {@link android.content.ContentProvider#getStreamTypes(Uri, String) getStreamTypes()} 864 s tr v <code>null</code>. 865 </p> 866 867 868 <!-- Implementing a Contract Class --> 869 <h2 id="ContractClass">Trin khai mt Lp Hp ng</h2> 870 <p> 871 Lp hp ng l mt lp <code>public final</code> cha cc nh ngha hng s cho 872 URI, tn ct, kiu MIME, v siu d liu khc lin quan ti trnh cung cp. Lp ny 873 s thit lp mt hp ng gia trnh cung cp v cc ng dng khc bng cch m bo rng trnh cung cp 874 c th c truy cp ng ngay c khi c thay i v gi tr thc s ca URI, tn ct, 875 v.v. 876 </p> 877 <p> 878 Lp hp ng cng gip cc nh pht trin v chng thng c tn d nh cho cc hng s ca mnh, 879 v vy cc nh pht trin t c kh nng s dng cc gi tr khng ng cho tn ct hay URI hn. Do l mt 880 lp, n c th cha ti liu Javadoc. Cc mi trng pht trin tch hp nh 881 Eclipse c th t ng in cc tn hng s t lp hp ng v hin th Javadoc cho cc 882 hng s . 883 </p> 884 <p> 885 Cc nh pht trin khng th truy cp tp lp ca lp hp ng t ng dng ca mnh, nhng h c th 886 lng l bin dch n vo ng dng ca h t mt tp <code>.jar</code> m bn cung cp. 887 </p> 888 <p> 889 Lp {@link android.provider.ContactsContract} v cc lp lng nhau ca n l cc v d v 890 lp hp ng. 891 </p> 892 <h2 id="Permissions">Trin khai Quyn ca Trnh cung cp Ni dung</h2> 893 <p> 894 Quyn v truy cp i vi tt c kha cnh trong h thng Android c m t chi tit trong 895 ch <a href="{@docRoot}guide/topics/security/security.html">Bo mt v Quyn</a>. 896 Ch <a href="{@docRoot}guide/topics/data/data-storage.html">Kho lu tr D liu</a> cng 897 m t bo mt v cc quyn c hiu lc cho nhiu loi kho lu tr khc nhau. 898 Ni tm li, cc im quan trng l: 899 </p> 900 <ul> 901 <li> 902 Theo mc nh, cc tp d liu c lu tr trn b nh trong ca thit b l d liu ring t 903 i vi ng dng v trnh cung cp ca bn. 904 </li> 905 <li> 906 Cc c s d liu {@link android.database.sqlite.SQLiteDatabase} m bn to l d liu ring t 907 i vi ng dng v trnh cung cp ca bn. 908 </li> 909 <li> 910 Theo mc nh, cc tp d liu m bn lu vo b nh ngoi l d liu <em>cng khai</em> v 911 <em>c c cng khai</em>. Bn khng th s dng mt trnh cung cp ni dung hn ch truy cp vo cc tp trong 912 b nh ngoi v cc ng dng khc c th s dng lnh gi API khc c v ghi chng. 913 </li> 914 <li> 915 Cc lnh gi phng php m hoc to tp hoc c s d liu SQLite trn b nh trong 916 ca thit b ca bn c th cp quyn truy cp c v ghi cho tt c ng dng khc. Nu bn 917 s dng mt tp hoc c s d liu ni b lm kho lu gi ca trnh cung cp ca mnh, v bn cp quyn truy cp 918 "c c cng khai" hoc "ghi c cng khai", quyn m bn t cho trnh cung cp ca mnh trong 919 bn k khai ca n s khng bo v d liu ca bn. Quyn truy cp mc nh cho cc tp v c s d liu trong 920 b nh trong l "ring t", v i vi kho lu gi ca trnh cung cp ca mnh, bn khng nn thay i iu ny. 921 </li> 922 </ul> 923 <p> 924 Nu bn mun s dng cc quyn ca trnh cung cp ni dung kim sot truy cp vo d liu ca mnh, khi bn nn 925 lu tr d liu ca mnh trong cc tp ni b, c s d liu SQLite, hoc "m my" (v d, 926 trn mt my ch t xa), v bn nn gi cc tp v c s d liu ring t cho ng dng ca mnh. 927 </p> 928 <h3>Trin khai quyn</h3> 929 <p> 930 Tt c ng dng u c th c t hoc ghi vo trnh cung cp ca bn, ngay c khi d liu lin quan 931 l d liu ring t, v theo mc nh, trnh cung cp ca bn khng c t quyn. thay i iu ny, 932 hy t quyn cho trnh cung cp ca bn trong tp bn k khai ca bn bng cch s dng cc thuc tnh hoc phn t 933 con ca phn t <code><a href="{@docRoot}guide/topics/manifest/provider-element.html"> 934 <provider></a></code>. Bn c th t quyn p dng cho ton b trnh cung cp, 935 hoc cho mt s bng, hoc thm ch cho mt s bn ghi, hoc c ba. 936 </p> 937 <p> 938 Bn nh ngha cc quyn cho trnh cung cp ca bn bng mt hoc nhiu phn t 939 <code><a href="{@docRoot}guide/topics/manifest/permission-element.html"> 940 <permission></a></code> trong tp bn k khai ca bn. 941 quyn l duy nht cho trnh cung cp ca bn, hy s dng phm vi kiu Java cho thuc tnh 942 <code><a href="{@docRoot}guide/topics/manifest/permission-element.html#nm"> 943 android:name</a></code>. V d, t tn quyn c 944 <code>com.example.app.provider.permission.READ_PROVIDER</code>. 945 946 </p> 947 <p> 948 Danh sch sau lit k phm vi cc quyn ca trnh cung cp, bt u vi cc quyn 949 p dng cho ton b trnh cung cp ri mi n cc quyn chi tit hn. 950 Cc quyn chi tit hn c u tin so vi cc quyn c phm vi rng hn: 951 </p> 952 <dl> 953 <dt> 954 Quyn c-ghi n l cp trnh cung cp 955 </dt> 956 <dd> 957 Mt quyn kim sot c quyn truy cp c v ghi cho ton b trnh cung cp, c quy nh 958 bng thuc tnh <code><a href="{@docRoot}guide/topics/manifest/provider-element.html#prmsn"> 959 android:permission</a></code> ca phn t 960 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html"> 961 <provider></a></code>. 962 </dd> 963 <dt> 964 Quyn c ghi tch ring cp trnh cung cp 965 </dt> 966 <dd> 967 Mt quyn c v mt quyn ghi cho ton b trnh cung cp. Bn ch nh chng 968 bng cc thuc tnh <code><a href="{@docRoot}guide/topics/manifest/provider-element.html#rprmsn"> 969 android:readPermission</a></code> v 970 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html#wprmsn"> 971 android:writePermission</a></code> ca phn t 972 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html"> 973 <provider></a></code>. Chng c u tin so vi quyn c yu cu bi 974 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html#prmsn"> 975 android:permission</a></code>. 976 </dd> 977 <dt> 978 Quyn cp ng dn 979 </dt> 980 <dd> 981 Quyn c, ghi, hoc c/ghi cho mt URI ni dung trong trnh cung cp ca bn. Bn ch nh 982 tng URI m bn mun kim sot bng mt phn t con 983 <code><a href="{@docRoot}guide/topics/manifest/path-permission-element.html"> 984 <path-permission></a></code> ca phn t 985 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html"> 986 <provider></a></code>. Vi mi mt URI ni dung m bn ch nh, bn c th ch nh mt 987 quyn c/ghi, quyn c, hoc quyn ghi, hoc c ba. Quyn c v 988 quyn ghi c u tin so vi quyn c/ghi. ng thi, quyn cp ng dn 989 s c u tin so vi quyn cp trnh cung cp. 990 </dd> 991 <dt> 992 Quyn tm thi 993 </dt> 994 <dd> 995 L cp quyn cho php truy cp tm thi vo mt ng dng, ngay c khi ng dng 996 khng c cc quyn thng c yu cu. Tnh nng truy cp 997 tm thi lm gim s quyn m mt ng dng phi yu cu trong 998 bn k khai ca mnh. Khi bn dng n cc quyn tm thi, nhng ng dng duy nht m cn 999 quyn "lu di" cho trnh cung cp ca bn l nhng ng dng lin tc truy cp tt c 1000 d liu ca bn. 1001 <p> 1002 Xt cc quyn bn cn trin khai mt trnh cung cp v ng dng e-mail khi bn 1003 mun cho php mt ng dng trnh xem nh bn ngoi hin th cc ti liu nh km dng nh t trnh cung cp 1004 ca bn. cp cho trnh xem nh quyn truy cp cn thit m khng cn yu cu quyn, 1005 hy thit lp cc quyn tm thi cho URI ni dung i vi nh. Thit k ng dng e-mail ca bn sao cho 1006 khi ngi dng mun hin th mt nh, ng dng s gi mt nh cha URI ni dung 1007 ca nh v c cho php ti trnh xem nh. Trnh xem nh khi c th 1008 truy vn trnh cung cp e-mail ca bn truy xut nh, ngay c khi trnh xem khng 1009 c quyn c bnh thng cho trnh cung cp ca bn. 1010 </p> 1011 <p> 1012 s dng cc quyn tm thi, hoc t thuc tnh 1013 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html#gprmsn"> 1014 android:grantUriPermissions</a></code> ca phn t 1015 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html"> 1016 <provider></a></code> hoc thm mt hoc nhiu phn t con 1017 <code><a href="{@docRoot}guide/topics/manifest/grant-uri-permission-element.html"> 1018 <grant-uri-permission></a></code> vo phn t 1019 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html"> 1020 <provider></a></code> ca bn. Nu bn s dng cc quyn tm thi, bn phi gi 1021 {@link android.content.Context#revokeUriPermission(Uri, int) 1022 Context.revokeUriPermission()} bt c khi no bn g b h tr cho mt URI ni dung khi 1023 trnh cung cp ca mnh, v URI ni dung s c lin kt vi mt quyn tm thi. 1024 </p> 1025 <p> 1026 Gi tr ca thuc tnh s xc nh trnh cung cp ca bn c cho php truy cp bao nhiu. 1027 Nu thuc tnh c t thnh <code>true</code>, khi h thng s cp quyn tm thi 1028 cho ton b trnh cung cp ca bn, khng ch mi quyn khc m c yu cu bi 1029 quyn cp trnh cung cp hoc cp ng dn ca bn. 1030 </p> 1031 <p> 1032 Nu c ny c t thnh <code>false</code>, khi bn phi thm cc phn t con 1033 <code><a href="{@docRoot}guide/topics/manifest/grant-uri-permission-element.html"> 1034 <grant-uri-permission></a></code> vo phn t 1035 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html"> 1036 <provider></a></code> ca mnh. Mi phn t con li quy nh URI ni dung hoc 1037 cc URI m truy cp tm thi c cp cho. 1038 </p> 1039 <p> 1040 y quyn truy cp tm thi cho mt ng dng, nh phi cha 1041 c {@link android.content.Intent#FLAG_GRANT_READ_URI_PERMISSION} hoc c 1042 {@link android.content.Intent#FLAG_GRANT_WRITE_URI_PERMISSION}, hoc c hai. Nhng quyn 1043 ny c t bng phng php {@link android.content.Intent#setFlags(int) setFlags()}. 1044 </p> 1045 <p> 1046 Nu thuc tnh <code><a href="{@docRoot}guide/topics/manifest/provider-element.html#gprmsn"> 1047 android:grantUriPermissions</a></code> khng c mt, gi s rng n l 1048 <code>false</code>. 1049 </p> 1050 </dd> 1051 </dl> 1052 1053 1054 1055 <!-- The Provider Element --> 1056 <h2 id="ProviderElement">Phn t <provider></h2> 1057 <p> 1058 Nh cc thnh phn {@link android.app.Activity} v {@link android.app.Service}, 1059 mt lp con ca {@link android.content.ContentProvider} 1060 phi c nh ngha trong tp bn k khai cho ng dng ca n bng cch s dng phn t 1061 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html"> 1062 <provider></a></code>. H thng Android nhn thng tin sau t 1063 phn t: 1064 <dl> 1065 <dt> 1066 Thm quyn 1067 (<a href="{@docRoot}guide/topics/manifest/provider-element.html#auth">{@code 1068 android:authorities}</a>) 1069 </dt> 1070 <dd> 1071 Cc tn biu tng nhn bit ton b trnh cung cp trong h thng. Thuc tnh 1072 ny c m t chi tit hn trong phn 1073 <a href="#ContentURI">Thit k URI Ni dung</a>. 1074 </dd> 1075 <dt> 1076 Tn lp ca trnh cung cp 1077 (<code> 1078 <a href="{@docRoot}guide/topics/manifest/provider-element.html#nm">android:name</a> 1079 </code>) 1080 </dt> 1081 <dd> 1082 Lp trin khai {@link android.content.ContentProvider}. Lp ny 1083 c m t chi tit hn trong phn 1084 <a href="#ContentProvider">Trin khai Lp Trnh cung cp Ni dung</a>. 1085 </dd> 1086 <dt> 1087 Quyn 1088 </dt> 1089 <dd> 1090 Nhng thuc tnh quy nh quyn m cc ng dng khc phi c truy cp 1091 d liu ca trnh cung cp: 1092 <ul> 1093 <li> 1094 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html#gprmsn"> 1095 android:grantUriPermssions</a></code>: C quyn tm thi. 1096 </li> 1097 <li> 1098 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html#prmsn"> 1099 android:permission</a></code>: Quyn c/ghi n l i vi ton b trnh cung cp. 1100 </li> 1101 <li> 1102 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html#rprmsn"> 1103 android:readPermission</a></code>: Quyn c i vi ton b trnh cung cp. 1104 </li> 1105 <li> 1106 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html#wprmsn"> 1107 android:writePermission</a></code>: Quyn ghi i vi ton b trnh cung cp. 1108 </li> 1109 </ul> 1110 <p> 1111 Cc quyn v thuc tnh tng ng ca chng c m t chi tit hn trong 1112 phn 1113 <a href="#Permissions">Trin khai Quyn ca Trnh cung cp Ni dung</a>. 1114 </p> 1115 </dd> 1116 <dt> 1117 Thuc tnh khi ng v kim sot 1118 </dt> 1119 <dd> 1120 Nhng thuc tnh ny xc nh cch v thi im h thng Android khi ng trnh cung cp, cc 1121 c tnh tin trnh ca trnh cung cp, v cc thit t v thi gian chy: 1122 <ul> 1123 <li> 1124 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html#enabled"> 1125 android:enabled</a></code>: C cho php h thng khi ng trnh cung cp. 1126 </li> 1127 <li> 1128 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html#exported"> 1129 android:exported</a></code>: C cho php cc ng dng s dng trnh cung cp ny. 1130 </li> 1131 <li> 1132 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html#init"> 1133 android:initOrder</a></code>: Th t m trnh cung cp nn c khi ng, 1134 so vi cc trnh cung cp khc trong cng tin trnh. 1135 </li> 1136 <li> 1137 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html#multi"> 1138 android:multiProcess</a></code>: C cho php h thng khi ng trnh cung cp 1139 trong cng tin trnh nh my khch gi. 1140 </li> 1141 <li> 1142 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html#proc"> 1143 android:process</a></code>: Tn ca tin trnh m trnh cung cp 1144 nn chy trong . 1145 </li> 1146 <li> 1147 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html#sync"> 1148 android:syncable</a></code>: C cho bit rng d liu ca trnh cung cp s c 1149 ng b vi d liu trn mt my ch. 1150 </li> 1151 </ul> 1152 <p> 1153 Cc thuc tnh c lp ti liu theo di y trong ch hng dn nh pht trin i vi phn t 1154 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html"> 1155 <provider></a></code> 1156 . 1157 </p> 1158 </dd> 1159 <dt> 1160 Cc thuc tnh thng tin 1161 </dt> 1162 <dd> 1163 Mt biu tng ty chn v nhn cho trnh cung cp: 1164 <ul> 1165 <li> 1166 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html#icon"> 1167 android:icon</a></code>: Mt ti nguyn c th v cha mt biu tng cho trnh cung cp. 1168 Biu tng xut hin bn cnh nhn ca trnh cung cp trong danh sch ng dng trong 1169 <em>Settings</em> > <em>Apps</em> > <em>All</em>. 1170 </li> 1171 <li> 1172 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html#label"> 1173 android:label</a></code>: Mt nhn thng tin m t trnh cung cp hoc d liu 1174 ca n, hoc c hai. Nhn xut hin trong danh sch ng dng trong 1175 <em>Settings</em> > <em>Apps</em> > <em>All</em>. 1176 </li> 1177 </ul> 1178 <p> 1179 Cc thuc tnh c lp ti liu theo di y trong ch hng dn nh pht trin i vi phn t 1180 <code><a href="{@docRoot}guide/topics/manifest/provider-element.html"> 1181 <provider></a></code>. 1182 </p> 1183 </dd> 1184 </dl> 1185 1186 <!-- Intent Access --> 1187 <h2 id="Intents"> nh v Truy cp D liu</h2> 1188 <p> 1189 Cc ng dng c th gin tip truy cp mt trnh cung cp ni dung bng mt {@link android.content.Intent}. 1190 ng dng khng gi bt k phng php no ca {@link android.content.ContentResolver} hoc 1191 {@link android.content.ContentProvider}. Thay vo , n s gi mt nh bt u mt hot ng, 1192 y thng l mt b phn trong ng dng ca chnh trnh cung cp. Hot ng ch ph trch 1193 truy xut v hin th d liu trong UI ca n. Ty vo hnh ng trong nh, hot ng 1194 ch cng c th nhc ngi dng thc hin sa i d liu ca trnh cung cp. 1195 Mt nh cng c th cha d liu "ph thm" m hot ng ch hin th 1196 trong UI; khi ngi dng c ty chn thay i d liu ny trc khi s dng n sa i 1197 d liu trong trnh cung cp. 1198 </p> 1199 <p> 1200 1201 </p> 1202 <p> 1203 Bn c th mun s dng truy cp nh gip m bo ton vn d liu. Trnh cung cp ca bn c th ph thuc vo 1204 vic chn, cp nht v xa d liu theo l-gic nghip v c quy nh cht ch. Trong 1205 trng hp nh vy, vic cho php cc ng dng khc trc tip sa i d liu ca bn c th dn n d liu 1206 khng hp l. Nu bn mun cc nh pht trin s dng truy cp nh, hy m bo lp ti liu theo di n tht k. 1207 Gii thch vi h ti sao truy cp nh s dng UI ng dng ca chnh bn li tt hn l c gng sa i 1208 d liu bng m ca h. 1209 </p> 1210 <p> 1211 Vic x l mt nh n nhm sa i d liu ca trnh cung cp ca bn khng khc vi 1212 vic x l cc nh khc. Bn c th tm hiu v vic s dng nh bng cch c ch 1213 <a href="{@docRoot}guide/components/intents-filters.html"> nh v B lc nh</a>. 1214 </p> 1215