1, 概述
content主要是和数据库进行交互,对数据库进行增删改查操作。
2, content命令
命令格式:
content<command>uri
命令列表:
命令 | 功能 | 实现方法 |
insert | 插入 | ContentProvider. insert |
delete | 删除 | delete |
update | 更新 | update |
query | 查询 | query |
3, 详细流程
所有adb命令的content方法最后都会通过Content.java执行,然后通过跨进程调用ContentProvider的方法完成。
Content的main方法如下,
public static void main(String[] args) {
Parser parser = new Parser(args);
Command command = parser.parseCommand();
if (command != null) {
command.execute();
}
}
Parser是内部类, parseCommand方法如下,
public Command parseCommand() {
try {
String operation = mTokenizer.nextArg();
if (ARGUMENT_INSERT.equals(operation)) {
return parseInsertCommand();
} else if (ARGUMENT_DELETE.equals(operation)) {
return parseDeleteCommand();
} else if (ARGUMENT_UPDATE.equals(operation)) {
return parseUpdateCommand();
} else if (ARGUMENT_QUERY.equals(operation)) {
return parseQueryCommand();
} else if (ARGUMENT_CALL.equals(operation)) {
return parseCallCommand();
} else if (ARGUMENT_READ.equals(operation)) {
return parseReadCommand();
} else {
throw new IllegalArgumentException("Unsupported operation: " + operation);
}
} catch (IllegalArgumentException iae) {
System.out.println(USAGE);
System.out.println("[ERROR] " + iae.getMessage());
return null;
}
}
不同的指令分别对应不同的方法, query指令调用的parseQueryCommand方法如下,
public QueryCommand parseQueryCommand() {
Uri uri = null;
int userId = UserHandle.USER_OWNER;
String[] projection = null;
String sort = null;
String where = null;
for (String argument; (argument = mTokenizer.nextArg())!= null;) {
if (ARGUMENT_URI.equals(argument)) {
uri = Uri.parse(argumentValueRequired(argument));
} else if (ARGUMENT_USER.equals(argument)) {
userId = Integer.parseInt(argumentValueRequired(argument));
} else if (ARGUMENT_WHERE.equals(argument)) {
where = argumentValueRequired(argument);
} else if (ARGUMENT_SORT.equals(argument)) {
sort = argumentValueRequired(argument);
} else if (ARGUMENT_PROJECTION.equals(argument)) {
projection = argumentValueRequired(argument).split("[\\s]*:[\\s]*");
} else {
throw new IllegalArgumentException("Unsupported argument: " + argument);
}
}
if (uri == null) {
throw new IllegalArgumentException("Content provider URI not specified."
+ " Did you specify --uri argument?");
}
return new QueryCommand(uri, userId, projection, where, sort);
}
不同的指令对应不同的Command子对象, query指令对应的是QueryCommand对象。
在main方法中,调用Command子对象的execute的方法, QueryCommand的execute方法如下,
@Override
public void onExecute(IContentProvider provider) throws Exception {
Cursor cursor = provider.query(resolveCallingPackage(), mUri, mProjection, mWhere,
null, mSortOrder, null);
if (cursor == null) {
System.out.println("No result found.");
return;
}
try {
if (cursor.moveToFirst()) {
int rowIndex = 0;
StringBuilder builder = new StringBuilder();
do {
builder.setLength(0);
builder.append("Row: ").append(rowIndex).append(" ");
rowIndex++;
final int columnCount = cursor.getColumnCount();
for (int i = 0; i < columnCount; i++) {
if (i > 0) {
builder.append(", ");
}
String columnName = cursor.getColumnName(i);
String columnValue = null;
final int columnIndex = cursor.getColumnIndex(columnName);
final int type = cursor.getType(columnIndex);
switch (type) {
case Cursor.FIELD_TYPE_FLOAT:
columnValue = String.valueOf(cursor.getFloat(columnIndex));
break;
case Cursor.FIELD_TYPE_INTEGER:
columnValue = String.valueOf(cursor.getLong(columnIndex));
break;
case Cursor.FIELD_TYPE_STRING:
columnValue = cursor.getString(columnIndex);
break;
case Cursor.FIELD_TYPE_BLOB:
columnValue = "BLOB";
break;
case Cursor.FIELD_TYPE_NULL:
columnValue = "NULL";
break;
}
builder.append(columnName).append("=").append(columnValue);
}
System.out.println(builder);
} while (cursor.moveToNext());
} else {
System.out.println("No result found.");
}
} finally {
cursor.close();
}
}
}
调用ContentProvider的query方法开始查询,将查询结果显示到adb 界面。