关闭

android ContentProvider

376人阅读 评论(0) 收藏 举报

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());

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:323993次
    • 积分:4774
    • 等级:
    • 排名:第6032名
    • 原创:141篇
    • 转载:77篇
    • 译文:13篇
    • 评论:60条
    文章分类
    最新评论