Android ContentProvider

参考:

http://www.cnblogs.com/linjiqin/archive/2011/05/28/2061396.html

http://aijiawang-126-com.iteye.com/blog/655268

http://blog.csdn.net/imyfriend/article/details/6589917

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Book {
    public static final String  DATABASE_NAME="books.db";
    public static final String    TABLE_NAME="book";
    public static final int  VERSION=1;
     
    public static final String ID="_id";
    public static final String TITLE="title";
    public static final String PRICE ="price";
     
    public static final String AUTHORITY="com.android.provider.book";
     
    public static final int ITEM=1;
    public static final int ITEM_ID=2;
     
    public static final String CONTENT_TYPE="vnd.android.cursor.dir/vnd.com.android.book";
     
    public static final String CONTENT_ITEM_TYPE="vnd.android.cursor.item/vnd.com.android.book";
    public static final Uri CONTENT_URI = Uri.parse("content://"+AUTHORITY+"/item");
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.android.bookgallery;
 
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
 
public class BookGallery extends Activity {
     
    private class MyrContentObserver extends ContentObserver {
 
        public MyrContentObserver() {
            super(new Handler());
        }
 
        public void onChange(final boolean selfChange) {
                Log.v("test", "Service ContentObserver received notification");
        }
 
    }
     
    MyrContentObserver mObserver;
    private void registMyContentObserver(){
        mObserver = new MyrContentObserver(); 
        getContentResolver().registerContentObserver(Book.CONTENT_URI, 
                true, mObserver); 
    }
     
    private void unregistMyContentObserver(){
         getContentResolver().unregisterContentObserver(mObserver);
    }
     
    /** Called when the activity is first created. */
    EditText etTitle;
    EditText etPrice;
    Button btnAdd;
    Button btnDelete;
    ListView lvBooks;
    ContentResolver resolver;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("BookGallery: onCreate.... ");
        setContentView(R.layout.main);
        etTitle = (EditText) findViewById(R.id.ettitle);
        etPrice = (EditText) findViewById(R.id.etprice);
        btnAdd = (Button) findViewById(R.id.add);
        btnDelete = (Button) findViewById(R.id.delete);
        lvBooks = (ListView) findViewById(R.id.booklist);
        resolver = getContentResolver();
        registMyContentObserver();
        Cursor cs = resolver.query(Book.CONTENT_URI, null, null, null, Book.ID
                + " ASC");
        CursorAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_2, cs, new String[] {
                        Book.TITLE, Book.PRICE }, new int[] {
                        android.R.id.text1, android.R.id.text2 });
        lvBooks.setAdapter(adapter);
        btnAdd.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ContentValues contentValues;
                try {
                    String countryText = String.valueOf(etTitle.getText());
                    String codeNum = String.valueOf(etPrice.getText());
                    contentValues = new ContentValues();
                    contentValues.put(Book.TITLE, countryText);
                    contentValues.put(Book.PRICE, codeNum);
                } catch (Exception e) {
                    contentValues = new ContentValues();
                }
                resolver.insert(Book.CONTENT_URI, contentValues);
 
            }
 
        });
 
        btnDelete.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Uri uri = ContentUris.withAppendedId(Book.CONTENT_URI, 2);
                System.out.println("btnDelete.setOnClickListener...."
                        + uri.toString());
                resolver.delete(uri, null, null);
 
            }
 
        });
    }
     
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        unregistMyContentObserver();
    }
}

  

  provider类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.android.provider.book;
 
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
 
public class BookProvider extends ContentProvider {
 
    public class BookDatabasehelper extends SQLiteOpenHelper {
 
        public BookDatabasehelper(Context context, String name,
                CursorFactory factory, int version) {
            super(context, name, factory, version);
            // TODO Auto-generated constructor stub
        }
 
        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            System.out.println("BookDatabasehelper onCreate... ");
            db.execSQL("CREATE TABLE " + Book.TABLE_NAME + " (" + Book.ID
                    + " INTEGER PRIMARY KEY," + Book.TITLE + " TEXT,"
                    + Book.PRICE + " TEXT" + ");");
 
        }
 
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            System.out.println("BookDatabasehelper onUpgrade... ");
            Log.w("BookDatabasehelper", "Upgrading database from version "
                    + oldVersion + " to " + newVersion
                    + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS notes");
            onCreate(db);
        }
 
    }
 
    public BookDatabasehelper databasehelper;
    public static final UriMatcher uriMatcher;
    static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(Book.AUTHORITY, "item", Book.ITEM);
        uriMatcher.addURI(Book.AUTHORITY, "item/#", Book.ITEM_ID);
    }
 
    @Override
    public int delete(Uri uri, String where, String[] args) {
        // TODO Auto-generated method stub
        System.out.println("BookProvider delete....");
        SQLiteDatabase db = databasehelper.getWritableDatabase();
        int count;
        switch (uriMatcher.match(uri)) {
        case Book.ITEM:
            count = db.delete(Book.TABLE_NAME, where, args);
            System.out.println("BookProvider delete....count" + count);
            break;
        case Book.ITEM_ID:
            String id = uri.getPathSegments().get(1);
            count = db.delete(Book.TABLE_NAME,
                    Book.ID
                            + "="
                            + id
                            + (!TextUtils.isEmpty(where) ? " AND (" + where
                                    + ‘)‘ : ""), args);
            System.out.println("BookProvider delete....count" + count + "id="
                    + id);
            break;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }
 
    @Override
    public String getType(Uri uri) {
        // TODO Auto-generated method stub
        System.out.println(" BookProvider getType...");
        int code = uriMatcher.match(uri);
        switch (code) {
        case Book.ITEM:
            System.out.println(" BookProvider getType..." + Book.CONTENT_TYPE);
            return Book.CONTENT_TYPE;
        case Book.ITEM_ID:
            System.out.println(" BookProvider getType..."
                    + Book.CONTENT_ITEM_TYPE);
            return Book.CONTENT_ITEM_TYPE;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
    }
 
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO Auto-generated method stub
        System.out.println("BookProvider insert...");
        SQLiteDatabase db = databasehelper.getWritableDatabase();
        long rowId;
        if (uriMatcher.match(uri) != Book.ITEM) {
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
        rowId = db.insert(Book.TABLE_NAME, Book.ID, values);
        if (rowId > 0) {
            Uri noteUri = ContentUris.withAppendedId(Book.CONTENT_URI, rowId);
            System.out.println("BookProvider insert  notifyChange");
            getContext().getContentResolver().notifyChange(noteUri, null);
            return noteUri;
        }
        throw new SQLException("Failed to insert row into " + uri);
    }
 
    @Override
    public boolean onCreate() {
        // TODO Auto-generated method stub
        System.out.println("BookProvider  onCreate:");
        databasehelper = new BookDatabasehelper(getContext(),
                Book.DATABASE_NAME, null, Book.VERSION);
        return true;
    }
 
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        // TODO Auto-generated method stub
        System.out.println("BookProvider  query:");
        SQLiteDatabase db = databasehelper.getReadableDatabase();
        Cursor c;
        switch (uriMatcher.match(uri)) {
        case Book.ITEM:
            c = db.query(Book.TABLE_NAME, projection, selection, selectionArgs,
                    null, null, sortOrder);
            System.out.println("BookProvider  query:" + Book.CONTENT_TYPE);
            break;
        case Book.ITEM_ID:
            String id = uri.getPathSegments().get(1);
            c = db.query(Book.TABLE_NAME, projection, Book.ID
                    + "="
                    + id
                    + (!TextUtils.isEmpty(selection) ? " AND (" + selection
                            + ‘)‘ : ""), selectionArgs, null, null, sortOrder);
            System.out.println("BookProvider  query:" + Book.CONTENT_ITEM_TYPE);
            break;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
        c.setNotificationUri(getContext().getContentResolver(), uri);
        return c;
    }
 
    @Override
    public int update(Uri uri, ContentValues values, String where, String[] args) {
        // TODO Auto-generated method stub
        System.out.println("BookProviderupdate.... ");
        SQLiteDatabase db = databasehelper.getWritableDatabase();
        int count;
        switch (uriMatcher.match(uri)) {
        case Book.ITEM:
            count = db.update(Book.TABLE_NAME, values, where, args);
            break;
        case Book.ITEM_ID:
            String id = uri.getPathSegments().get(1);
            count = db.update(Book.TABLE_NAME, values,
                    Book.ID
                            + "="
                            + id
                            + (!TextUtils.isEmpty(where) ? " AND (" + where
                                    + ‘)‘ : ""), args);
            break;
        default:
            throw new IllegalArgumentException("Unknown URI " + uri);
        }
        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }
 
}

  

1
2
3
4
5
6
7
8
9
10
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.android.provider.book"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
 
      <provider android:name="BookProvider"  android:authorities="com.android.provider.book" />
    </application>
    <uses-sdk android:minSdkVersion="3" />
 
</manifest>

  

 

要点:

getType(Uri uri)

单item vnd.android.cursor.item 

多item  vnd.android.cursor.dir/ 

Implement this to handle requests for the MIME type of the data at the given URI. The returned MIME type should start with vnd.android.cursor.item for a single record, or vnd.android.cursor.dir/ for multiple items. This method can be called from multiple threads, as described in Processes and Threads.

Note that there are no permissions needed for an application to access this information; if your content provider requires read and/or write permissions, or is not exported, all applications can still call this method regardless of their access permissions. This allows them to retrieve the MIME type for a URI when dispatching intents.

getContentResolver().notifyChange(uri, null) contentobserver 观察者

 

Android ContentProvider,布布扣,bubuko.com

Android ContentProvider

上一篇:阿里产品专家:高情商的技术人,如何做沟通?


下一篇:Android(01)- 基本概念以及开发环境