@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) {
SQLiteDatabase 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
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
![](https://i-blog.csdnimg.cn/blog_migrate/7f5c4da23d77ef41710ee77d8dfe08ca.jpeg)
最后
我这里整理了一份完整的学习思维以及Android开发知识大全PDF。
当然实践出真知,即使有了学习线路也要注重实践,学习过的内容只有结合实操才算是真正的掌握。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!**
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
![](https://i-blog.csdnimg.cn/blog_migrate/7f5c4da23d77ef41710ee77d8dfe08ca.jpeg)
最后
我这里整理了一份完整的学习思维以及Android开发知识大全PDF。
[外链图片转存中…(img-rffhubSH-1712724657454)]
当然实践出真知,即使有了学习线路也要注重实践,学习过的内容只有结合实操才算是真正的掌握。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!