SmsProvider, MmsProvider, MmsSmsProvider利用MmsSmsDatabaseHelper来操作数据库。
1. MmsSmsDatabaseHelper继承了SQLiteOpenHelper。
public class MmsSmsDatabaseHelper extends SQLiteOpenHelper
它至少需要实现三个方法:构造方法、onCreate方法、onUpdate方法。
2. 实现私有构造方法。
private MmsSmsDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mContext = context;
}
3. 通过getInstance()方法获取MmsSmsDatabaseHelper的一个实例。
static synchronized MmsSmsDatabaseHelper getInstance(Context context) {
if (sInstance == null) {
sInstance = new MmsSmsDatabaseHelper(context);
}
return sInstance;
}
4. 在onCreate()方法中创建数据库表、触发器、索引。
@Override
public void onCreate(SQLiteDatabase db) {
createMmsTables(db);
createSmsTables(db);
createCommonTables(db);
createCommonTriggers(db);
createMmsTriggers(db);
createWordsTables(db);
createIndices(db);
}
5. 实现了onUpgrade()方法。
public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion)
6. 在createCommonTables(db)方法中
(1)创建表threads表。
This table maps the subject and an ordered set of recipient IDs, separated by spaces, to a unique thread ID. The IDs come from the canonical_addresses table. This works because messages are considered to be part of the same thread if they have the same subject (or a null subject) and the same set of recipients.
db.execSQL("CREATE TABLE threads (" +
Threads._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
Threads.DATE + " INTEGER DEFAULT 0," +
Threads.MESSAGE_COUNT + " INTEGER DEFAULT 0," +
Threads.RECIPIENT_IDS + " TEXT," +
Threads.SNIPPET + " TEXT," +
Threads.SNIPPET_CHARSET + " INTEGER DEFAULT 0," +
Threads.READ + " INTEGER DEFAULT 1," +
Threads.TYPE + " INTEGER DEFAULT 0," +
Threads.ERROR + " INTEGER DEFAULT 0," +
Threads.HAS_ATTACHMENT + " INTEGER DEFAULT 0);");
(2)创建canonical_addresses表。
This table maps the first instance seen of any particular MMS/SMS address to an ID, which is then used as its canonical(标准的,权威的) representation. If the same address or an equivalent address (as determined by our Sqlite PHONE_NUMBERS_EQUAL extension) is seen later, this same ID will be used. The _id is created with AUTOINCREMENT so it will never be reused again if a recipient is deleted.
db.execSQL("CREATE TABLE canonical_addresses (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"address TEXT);");
(3)创建sms表。
字段status解析:a TP-Status value or -1 if it status hasn't been received。
db.execSQL("CREATE TABLE sms (" +
"_id INTEGER PRIMARY KEY," +
"thread_id INTEGER," +
"address TEXT," +
"person INTEGER," +
"date INTEGER," +
"date_sent INTEGER DEFAULT 0," +
"protocol INTEGER," +
"read INTEGER DEFAULT 0," +
"status INTEGER DEFAULT -1," +
"type INTEGER," +
"reply_path_present INTEGER," +
"subject TEXT," +
"body TEXT," +
"service_center TEXT," +
"locked INTEGER DEFAULT 0," +
"error_code INTEGER DEFAULT 0," +
"seen INTEGER DEFAULT 0" +
");");