Android的使用ContentProvider,下面有代码,可以运行,4面阿里拿到P7Offer

@Override

public void onClick(View v) {

ContentResolver resolver = getContentResolver();

if(v.getId() == R.id.query_all) {

Cursor cursor = resolver.query(NameListProvider.all_name_uri, null, null, null,null);

boolean hasFirst = cursor.moveToFirst();

try {

if(hasFirst) {

do {

String pkg = cursor.getString(cursor.getColumnIndex(“pkg”));

String type = cursor.getString(cursor.getColumnIndex(“type”));

// Log.i(TAG, " pkg=" + pkg + " type=" + type);

s+=“pkg=”+pkg+"\n" +“type=”+type+"\n";

} while (cursor.moveToNext());

TV_query_all.setText(s);

}else {

Log.i(TAG, “无数据,请先初始化数据”);

}

} finally {

cursor.close();

}

try {

Thread.currentThread().sleep(10 * 1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}else if(v.getId() == R.id.remove_all) {

int rowId = resolver.delete(NameListProvider.all_name_uri, null, null);

}else if(v.getId() == R.id.init_all) {

Intent i = new Intent();

i.setAction(“cn.nubia.small_window_namelist_update”);

sendBroadcast(i);

}else if(v.getId() == R.id.add_one) {//add_one

ContentValues values = new ContentValues();

if (add_one1.getText().toString().equals("")&&add_one2.getText().toString().equals("")){

Toast.makeText(this, “请输入pkg和type”, Toast.LENGTH_SHORT).show();

}else {

values.put(“pkg”, add_one1.getText().toString());

values.put(“type”,add_one2.getText().toString());

resolver.insert(NameListProvider.all_name_uri, values);

}

}else if(v.getId() == R.id.remove_one) {//add_one

String where = “pkg = ?”;

String[] args = new String[] {“test_add_one”};

int rowId = resolver.delete(NameListProvider.all_name_uri, where, args);

}

}

}

2.NameListDBHelper.java中:

public class NameListDBHelper extends SQLiteOpenHelper {

private final static String TAG = “NameListDataBaseHelper”;

private static NameListDBHelper mInstance;

private final String CREATE_NAME_LIST_TABLE = “create table " + TABLE_NAME + " (” +

"id integer primary key autoincrement, " +

"pkg text, " +

“type text)”;

private final static int DB_VERSION = 1;

public final static String TABLE_NAME = “smallwindow”;

public NameListDBHelper(Context context, String name, CursorFactory factory, int version) {

super(context, name, factory, version);

}

public static synchronized NameListDBHelper getInstance(Context context) {

if(mInstance == null) {

mInstance = new NameListDBHelper(context, “name_list.db”, null, DB_VERSION);

}

return mInstance;

}

@Override

public void onCreate(SQLiteDatabase db) {

Log.i(TAG, “onCreate DB , DB_VERSION=” + DB_VERSION);

db.execSQL(CREATE_NAME_LIST_TABLE);

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

Log.i(TAG, “onUpgrade , oldVersion=” + oldVersion + " newVersion=" + newVersion);

}

public void add(List infos) {

if(infos != null) {

SQL

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

iteDatabase db = mInstance.getWritableDatabase();

db.delete(TABLE_NAME, null, null);

for (AppInfo info : infos) {

ContentValues values = new ContentValues();

values.clear();

values.put(“pkg”, info.getPkg());

values.put(“type”, info.getType());

db.insert(TABLE_NAME, null, values);

}

}

}

}

3.NameListProvider.java中:

public class NameListProvider extends ContentProvider {

private final static String TAG = “NameListProvider”;

private SQLiteDatabase db = null;

private static UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);

private final static String AUTHORITY = “cn.nubia.smallwindow.provider”;

private final static int APPINFO = 1;

private final static int APPINFOS = 2;

private final static String TABLE = NameListDBHelper.TABLE_NAME;

public final static Uri all_name_uri = Uri.parse(“content://” + AUTHORITY + “/appinfo”);

static {

MATCHER.addURI(AUTHORITY, “appinfo/#”, APPINFO);

MATCHER.addURI(AUTHORITY, “appinfo”, APPINFOS);

}

@Override

public boolean onCreate() {

db = NameListDBHelper.getInstance(getContext()).getWritableDatabase();

return db != null;

}

@Nullable

@Override

public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

int matchType = MATCHER.match(uri);

Cursor cursor = null;

switch (matchType) {

case APPINFO:

String queryPkg = uri.getPathSegments().get(1);

cursor = db.query(TABLE, projection, “pkg=?”, new String[] {queryPkg}, null, null, null);

break;

case APPINFOS:

cursor = db.query(TABLE, projection, selection, selectionArgs, null, null, null);

break;

default:

break;

}

return cursor;

}

@Nullable

@Override

public String getType(Uri uri) {

switch(MATCHER.match(uri)){

case APPINFO:

return “vnd.android.cursor.dir/appinfo”;

case APPINFOS:

return “vnd.android.cursor.item/appinfo”;

default:

throw new IllegalArgumentException(“Unknow Uri:”+uri.toString());

}

}

@Nullable

@Override

public Uri insert(Uri uri, ContentValues values) {

Log.i(TAG, “insert”);

int matchType = MATCHER.match(uri);

Log.i(TAG, “insert matchType=” + matchType);

switch (matchType) {

case APPINFO:

case APPINFOS:

long rowId = db.insert(TABLE, null, values);

if(rowId == -1) {

Log.i(TAG, “insert failed”);

}

break;

default:

break;

}

getContext().getContentResolver().notifyChange(uri, null);

return null;

}

@Override

public int delete(Uri uri, String selection, String[] selectionArgs) {

int matchType = MATCHER.match(uri);

int rowId = 0;

switch (matchType) {

case APPINFO:

case APPINFOS:

rowId = db.delete(TABLE, selection, selectionArgs);

if(rowId == -1) {

Log.i(TAG, “delete failed”);

}

break;

default:

break;

}

getContext().getContentResolver().notifyChange(uri, null);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值