android ContentProvider

1

package com.android.test.broadcast;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

public class MyContentProvider extends ContentProvider{

  private SQLiteDatabase mySqlDB;
  private DatabaseHelper mDBHelpler;
  private static final String DATABASE_NAME = "Users.db";
  private static final int DATABASE_VERSION = 1;
  private static final String TABLE_NAME = "contcats";
  private static final String TAG ="MyContentProvider";

  private static class DatabaseHelper extends SQLiteOpenHelper {
  DatabaseHelper(Context context) {
  super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
   db.execSQL("Create table " + TABLE_NAME + "( _id INTEGER PRIMARY KEY AUTOINCREMENT, CONTACTS_NAME TEXT,CONTACTS_SEX TEXT,CONTACTS_AGE INTEGER);");
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
  onCreate(db);
  }
  }

  @Override
  public int delete(Uri uri, String s, String[] as) {
  return 0;
  }

  @Override
  public String getType(Uri uri) {
  return null;
  }

  @Override
  public Uri insert(Uri uri, ContentValues contentvalues) {
   mySqlDB = mDBHelpler.getWritableDatabase();
  long rowId = mySqlDB.insert(TABLE_NAME, "", contentvalues);
  if (rowId > 0) {
  Uri rowUri = ContentUris.appendId(Users.User.ContentUri.buildUpon(), rowId).build();
  getContext().getContentResolver().notifyChange(rowUri, null);
  return rowUri;
  }
  throw new SQLException("Failed to insert row into " + uri);
  }

  @Override
  public boolean onCreate() {
   mDBHelpler = new DatabaseHelper(getContext());
  return (mDBHelpler == null) ? false : true;
  }

  @Override
  public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
  SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
  SQLiteDatabase db = mDBHelpler.getReadableDatabase();
  qb.setTables(TABLE_NAME);
  Cursor c = qb.query(db, projection, selection, null, null, null, sortOrder);
  c.setNotificationUri(getContext().getContentResolver(), uri);
  return c;
  }

  @Override
  public int update(Uri uri, ContentValues contentvalues, String s, String[] as) {
  return 0;
  }

}

22222222222222222222

package com.android.test.broadcast;

import android.net.Uri;
import android.provider.BaseColumns;

public class Users {
 
 public static String user="user";
 public static final String author="com.android.test.broadcast";
 public static final class User implements BaseColumns {
  public static final  Uri ContentUri=Uri.parse("content://com.android.test.broadcast");
  public static final String USER_NAME="user";
 }

}

3333333333333333333333333

<provider android:name="com.android.test.broadcast.MyContentProvider" android:authorities="com.android.test.broadcast"></provider>

 

ContentValues values=new ContentValues();
  values.put("_id", 1);
  values.put("CONTACTS_AGE",21);
  values.put("CONTACTS_SEX", "0");
  values.put("CONTACTS_NAME","hello");
  mContext.getContentResolver().insert(Uri.parse("content://com.android.test.broadcast"), values);
  
  ContentValues values1=new ContentValues();
  values1.put("_id", 2);
  values1.put("CONTACTS_AGE",22);//CONTACTS_AGE
  values1.put("CONTACTS_SEX", "1");//CONTACTS_SEX
  values1.put("CONTACTS_NAME","test");//CONTACTS_NAME
  mContext.getContentResolver().insert(Uri.parse("content://com.android.test.broadcast"), values1);
  
  Cursor myCursor=mContext.getContentResolver().query(Uri.parse("content://com.android.test.broadcast"),null, null,null,null);
  Log.i("============","=========="+myCursor.getCount());

你可能感兴趣的:(android ContentProvider)