Content Provider contentprovider

Source: Internet
Author: User

 

  1. First, add the content provider code to the application that needs to provide the content provider.

     

  2. Package com. example. sqllite;

  3.  

  4. Import com. example. sqllite. servise. dbopenhelp;

  5.  

  6. Import Android. content. contentprovider;

  7. Import Android. content. contenturis;

  8. Import Android. content. contentvalues;

  9. Import Android. content. urimatcher;

  10. Import Android. database. cursor;

  11. Import Android. database. SQLite. sqlitedatabase;

  12. Import android.net. Uri;

  13.  

  14. Public class personprovider extends contentprovider {

  15.  

  16. Private dbopenhelp;

  17. // URI matching to check whether the request matches

  18. Private Static final urimatcher matcher = new urimatcher (urimatcher. no_match );

  19. Private Static final int success = 1;

  20. Static {

  21. Matcher. adduri ("com. contentprovide. providers. personprovides", "person", success );

  22. }

  23.  

  24. @ Override

  25. Public Boolean oncreate (){

  26. Dbopenhelp = new dbopenhelp (this. getcontext ());

  27. Return true;

  28. }

  29. @ Override

  30. Public int Delete (URI Uri, string selection, string [] selectionargs ){

  31. // Todo auto-generated method stub

  32. Return 0;

  33. }

  34.  

  35. @ Override

  36. Public String GetType (URI ){

  37. // Todo auto-generated method stub

  38. Return NULL;

  39. }

  40.  

  41. @ Override

  42. Public URI insert (URI Uri, contentvalues values ){

  43. Sqlitedatabase DB = dbopenhelp. getwritabledatabase ();

  44. Switch (matcher. Match (URI )){

  45. Case success:

  46. Long rowid = dB. insert ("person", "name", values );

  47. // URI inserturi = URI. parse ("content: // com. contentprovide. providers. personprovides/person/" + rowid );

  48. Uri inserturi = contenturis. withappendedid (Uri, rowid );

  49. DB. Close ();

  50. Return inserturi;

  51.  

  52. Default:

  53. Throw new illegalargumentexception ("unknown UIR ");

  54. }

  55. }

  56.  

  57.  

  58.  

  59. @ Override

  60. Public cursor query (URI Uri, string [] projection, string selection,

  61. String [] selectionargs, string sortorder ){

  62. // Todo auto-generated method stub

  63. Return NULL;

  64. }

  65.  

  66. @ Override

  67. Public int Update (URI Uri, contentvalues values, string selection,

  68. String [] selectionargs ){

  69. // Todo auto-generated method stub

  70. Return 0;

  71. }

  72.  

  73. }

2. Add configuration items to the original application

<? XML version = "1.0" encoding = "UTF-8"?>

<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"

Package = "com. example. sqllite"

Android: versioncode = "1"

Android: versionname = "1.0" type = "codeph" text = "/codeph">

 

<Uses-SDK

Android: minsdkversion = "8"

Android: targetsdkversion = "18"/>

 

<Application

Android: allowbackup = "true"

Android: icon = "@ drawable/ic_launcher"

Android: Label = "@ string/app_name"

Android: theme = "@ style/apptheme">

<Activity

Android: Name = "com. example. sqllite. mainactivity"

Android: Label = "@ string/app_name">

<Intent-filter>

<Action Android: Name = "android. Intent. Action. Main"/>

 

<Category Android: Name = "android. Intent. Category. launcher"/>

</Intent-filter>

</Activity>

<! -- Add content provider configuration -->

<Provider Android: Name = ". personprovider" Android: Authorities = "com. contentprovide. providers. personprovides"/>

</Application>

 

</Manifest>

3. access in the new application

Uri uri = URI. parse ("content: // com. contentprovide. providers. personprovides/person ");

// Access the help class of contentprovider

Contentresolver resolver = This. getapplicationcontext (). getcontentresolver ();

Contentvalues values = new contentvalues ();

Values. Put ("name", "hahha ");

Values. Put ("Age", 33 );

Values. Put ("amount", "123 ");

Resolver. insert (Uri, values );

 

 

Content Provider contentprovider

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.