码迷,mamicode.com
首页 > 其他好文 > 详细

内容提供者ContentProvider

时间:2014-09-11 19:16:52      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   http   io   os   ar   sp   cti   

 

  1. 首先在需要提供内容提供者的应用中添加内容提供者的代码

     

  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 dbOpenHelp;

  17. //Uri匹配器,检验是否匹配

  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 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("未知的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.在原应用中添加配置项

<?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" >

 

    <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>

        

        <!--  添加内容提供者的配置 -->

       <provider android:name=".personProvider" android:authorities="com.contentProvide.providers.personProvides"/>

    

    </application>

 

</manifest>


3.新应用中进行访问

Uri uri=Uri.parse("content://com.contentProvide.providers.personProvides/person");

//访问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);

 


 

内容提供者ContentProvider

标签:des   android   style   http   io   os   ar   sp   cti   

原文地址:http://www.cnblogs.com/zhangjie9142/p/3967046.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!