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

008_02_ContentProvider

时间:2015-05-20 00:29:47      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

什么是ContentProvider(内容提供者) ?
 
Content Provider维护特定的应用数据,并可以让其它应用轻松访问该数据。对数据使用者来说它是数据提供者。它提供统一的接口对数据进行操作,使用者不用关心数据到底是如何存储的以及数据类型到底是什么。也就是说,Content Provider作为数据提供者,提供了对外共享私有数据一种机制,使Android应用能方便地基于该机制进行数据访问。
技术分享
 1 package com.example.mydbopenhelper;
 2 
 3 import android.content.ContentValues;
 4 import android.content.Context;
 5 import android.database.sqlite.SQLiteDatabase;
 6 import android.database.sqlite.SQLiteDatabase.CursorFactory;
 7 import android.database.sqlite.SQLiteOpenHelper;
 8 import android.text.AlteredCharSequence;
 9 import android.util.Log;
10 
11 public class MydbOpenHelper extends SQLiteOpenHelper {
12     public MydbOpenHelper(Context context, String name, CursorFactory factory,
13             int version) {
14         super(context, name, factory, version);
15     }
16     @Override
17     public void onCreate(SQLiteDatabase db) {
18         String createtable = "create table userphonenum(id int, name varchar(20), telephone char(11));";
19         db.execSQL(createtable);  
20         ContentValues c = new ContentValues();
21         //id int ,name string ,password string
22         for(int i=1;i<5;i++){
23             c.put("id", i);
24             c.put("name", "user"+i);
25             c.put("telephone", "1371111111"+i);     
26             db.insert("userphonenum", null, c);
27         }
28         System.out.println("MydbOpenHelper.onCreate()");
29     }
30     @Override
31     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {    
32         String createtable = "create table userwechat(id int, name varchar(20), wechat char(11));";
33         db.execSQL(createtable);
34         ContentValues c = new ContentValues();
35         //id int ,name string ,password string
36         for(int i=1;i<5;i++){
37             c.put("id", i);
38             c.put("name", "user"+i);
39             c.put("wechat", "weixin00"+i);
40             db.insert("userwechat", null, c);
41         }
42         System.out.println("MydbOpenHelper.onCreate()");    
43         Log.i("MydbOpenHelper", "oldervserion"+oldVersion+"newversion"+newVersion);
44         System.out.println("MydbOpenHelper.onUpgrade()");
45     }
46 }
MydbOpenHelper.java

 

技术分享
 1 package com.example.myprovider;
 2 
 3 import java.util.List;
 4 
 5 import com.example.mydbopenhelper.MydbOpenHelper;
 6 
 7 import android.content.ContentProvider;
 8 import android.content.ContentUris;
 9 import android.content.ContentValues;
10 import android.content.UriMatcher;
11 import android.database.Cursor;
12 import android.database.sqlite.SQLiteDatabase;
13 import android.net.Uri;
14 
15 public class MyContentProvider extends ContentProvider{
16     SQLiteDatabase db;
17     static UriMatcher um = new UriMatcher(UriMatcher.NO_MATCH);
18     static{
19         um.addURI("com.example.mycontentprovider", "userphonenum", 1);
20         um.addURI("com.example.mycontentprovider", "userwechat", 2);
21         um.addURI("com.example.mycontentprovider", "userwechat/#", 3);
22         um.addURI("com.example.mycontentprovider", "userwechat/*", 4);
23         um.addURI("com.example.mycontentprovider", "userwechat/*/#", 5);
24     }
25     @Override
26     public boolean onCreate() {
27         MydbOpenHelper helper = new MydbOpenHelper(getContext(), "userinfo.db", null, 2);
28         db = helper.getReadableDatabase();
29         return false;
30     }
31     @Override
32     public Cursor query(Uri uri, String[] projection, String selection,
33             String[] selectionArgs, String sortOrder) {
34         Cursor c = null;
35         if(um.match(uri) == 1){
36             c = db.query("userphoneum", projection, selection, selectionArgs, null, null, null);
37         }
38         else if(um.match(uri)==2){}
39         else if(um.match(uri)==3){
40             long l = ContentUris.parseId(uri);
41             c = db.query("userphonenum", projection, selection, selectionArgs, null, null, null);
42         }
43         else if(um.match(uri) ==4){
44             List<String> l = uri.getPathSegments();
45             String agr = l.get(1);    
46             c = db.query("userwechat",new String[] {agr}, null, null, null, null, null);
47         }
48         return c;
49     }
50 
51     @Override
52     public String getType(Uri uri) {
53         // TODO Auto-generated method stub
54         return null;
55     }
56 
57     @Override
58     public Uri insert(Uri uri, ContentValues values) {
59         //判断用户需要插入的到底是哪个数据库
60         if(um.match(uri)==1){
61             db.insert("userphonenum", null, values);
62         }
63         else if(um.match(uri) == 2){
64             db.insert("userwechat", null, values);
65         }
66         return null;
67     }
68 
69     @Override
70     public int delete(Uri uri, String selection, String[] selectionArgs) {
71         db.delete("userphonenum", selection, selectionArgs);
72         return 0;
73     }
74 
75     @Override
76     public int update(Uri uri, ContentValues values, String selection,
77             String[] selectionArgs) {
78         db.update("userphonenum", values, selection, selectionArgs);
79         return 0;
80     }
81 
82 }
MyContentProvider.java

 

技术分享
 1 package com.example.day08_contentprovider;
 2 
 3 import com.example.mydbopenhelper.MydbOpenHelper;
 4 
 5 import android.app.Activity;
 6 import android.database.sqlite.SQLiteDatabase;
 7 import android.os.Bundle;
 8 import android.view.Menu;
 9 import android.view.MenuItem;
10 
11 public class MainActivity extends Activity {
12 
13     @Override
14     protected void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.activity_main);
17         
18         MydbOpenHelper helper = new MydbOpenHelper(this, "userinfo.db", null, 2);
19         SQLiteDatabase db = helper.getReadableDatabase();
20     }
21 }
MainActivity.java

 

技术分享
1         <provider
2             android:name="com.example.myprovider.MyContentProvider"
3             android:authorities="com.example.mycontentprovider"
4             android:exported="true">
5         </provider>
AndroidManifest.xml

 

 

008_02_ContentProvider

标签:

原文地址:http://www.cnblogs.com/woodrow2015/p/4512465.html

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