如何将 SMS 消息存储到 SQLite 数据库?
How to store SMS message to SQLite Database?
我想在收到消息后将其存储到 SQLite
数据库中。我知道我必须使用 BroadcastReceiver
来做到这一点。但我不知道该怎么做。调用 onReceive()
时是否可以将传入的短信存储到数据库中?
请帮助我。
是的,您可以将消息存储到 sqlite 数据库中。
这是 onReceive() 方法代码:-
@Override
public void onReceive(Context context, Intent intent) {
//—get the SMS message passed in—
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String messages = "";
if (bundle != null) {
//—retrieve the SMS message received—
Object[] smsExtra = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[smsExtra.length];
for (int i = 0; i < msgs.length; i++) {
SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);
//take out content from sms
String body = sms.getMessageBody().toString();
String address = sms.getOriginatingAddress();
messages += "SMS from" + address + ":\n";
messages += body + "\n";
putSmsToDatabase(sms, context);
}
//—display the new SMS message—
Toast.makeText(context, messages, Toast.LENGTH_SHORT).show();
}
}
private void putSmsToDatabase(SmsMessage sms, Context context) {
DataBaseHelper dataBaseHelper = new DataBaseHelper(context);
SQLiteDatabase db = dataBaseHelper.getWritableDatabase();
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
// Create SMS row
ContentValues values = new ContentValues();
values.put("address", sms.getOriginatingAddress().toString());
values.put("date", mydate);
values.put("body", sms.getMessageBody().toString());
// values.put( READ, MESSAGE_IS_NOT_READ );
// values.put( STATUS, sms.getStatus() );
// values.put( TYPE, MESSAGE_TYPE_INBOX );
// values.put( SEEN, MESSAGE_IS_NOT_SEEN );
db.insert("datatable", null, values);
db.close();
}
完整教程是:-
希望对您有所帮助:)
不需要存入数据库,直接从
中获取即可
Uri myMessage = Uri.parse("content://sms/");
ContentResolver cr = this.getContentResolver();
Cursor c = cr.query(myMessage, new String[] { "_id", "address", "date",
"body", "read" }, null, null, null);
startManagingCursor(c);
if (cursor.moveToNext()) {
smsLogsIncoming = new HashSet<SmsLogData>();
smsLogsOutGoing = new HashSet<SmsLogData>();
// timeLastChecked = cursor.getLong(cursor.getColumnIndex("date"));
simNumber = tm.getLine1Number();
do {
date = cursor.getLong(cursor.getColumnIndex("date"));
address = cursor.getString(cursor.getColumnIndex("address"));
body = cursor.getString(cursor.getColumnIndex("body"));
String type = cursor.getString(cursor.getColumnIndex("type"));
} while (cursor.moveToNext());
}
cursor.close();
我想在收到消息后将其存储到 SQLite
数据库中。我知道我必须使用 BroadcastReceiver
来做到这一点。但我不知道该怎么做。调用 onReceive()
时是否可以将传入的短信存储到数据库中?
请帮助我。
是的,您可以将消息存储到 sqlite 数据库中。
这是 onReceive() 方法代码:-
@Override
public void onReceive(Context context, Intent intent) {
//—get the SMS message passed in—
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String messages = "";
if (bundle != null) {
//—retrieve the SMS message received—
Object[] smsExtra = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[smsExtra.length];
for (int i = 0; i < msgs.length; i++) {
SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);
//take out content from sms
String body = sms.getMessageBody().toString();
String address = sms.getOriginatingAddress();
messages += "SMS from" + address + ":\n";
messages += body + "\n";
putSmsToDatabase(sms, context);
}
//—display the new SMS message—
Toast.makeText(context, messages, Toast.LENGTH_SHORT).show();
}
}
private void putSmsToDatabase(SmsMessage sms, Context context) {
DataBaseHelper dataBaseHelper = new DataBaseHelper(context);
SQLiteDatabase db = dataBaseHelper.getWritableDatabase();
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
// Create SMS row
ContentValues values = new ContentValues();
values.put("address", sms.getOriginatingAddress().toString());
values.put("date", mydate);
values.put("body", sms.getMessageBody().toString());
// values.put( READ, MESSAGE_IS_NOT_READ );
// values.put( STATUS, sms.getStatus() );
// values.put( TYPE, MESSAGE_TYPE_INBOX );
// values.put( SEEN, MESSAGE_IS_NOT_SEEN );
db.insert("datatable", null, values);
db.close();
}
完整教程是:-
希望对您有所帮助:)
不需要存入数据库,直接从
中获取即可Uri myMessage = Uri.parse("content://sms/");
ContentResolver cr = this.getContentResolver();
Cursor c = cr.query(myMessage, new String[] { "_id", "address", "date",
"body", "read" }, null, null, null);
startManagingCursor(c);
if (cursor.moveToNext()) {
smsLogsIncoming = new HashSet<SmsLogData>();
smsLogsOutGoing = new HashSet<SmsLogData>();
// timeLastChecked = cursor.getLong(cursor.getColumnIndex("date"));
simNumber = tm.getLine1Number();
do {
date = cursor.getLong(cursor.getColumnIndex("date"));
address = cursor.getString(cursor.getColumnIndex("address"));
body = cursor.getString(cursor.getColumnIndex("body"));
String type = cursor.getString(cursor.getColumnIndex("type"));
} while (cursor.moveToNext());
}
cursor.close();