Praveen Kumar
Praveen Kumar

Reputation: 61

How to store SMS message to SQLite Database?

I want to store the messages into SQLite database when they receive. I know I have to use BroadcastReceiver to do this. But I don't know how to do this. Is it possible to store incoming sms into database when onReceive() is called?
Please help me.

Upvotes: 2

Views: 7958

Answers (2)

Biswajit
Biswajit

Reputation: 1869

Yes you can store the messages into sqlite database.

This is the onReceive() method code :-

@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();
}

and the full tutorial is :-

http://androidexample.com/Incomming_SMS_Broadcast_Receiver_-_Android_Example/index.php?view=article_discription&aid=62&aaid=87

Hope this help you :)

Upvotes: 6

Srishti Roy
Srishti Roy

Reputation: 576

You donot need to save it in database,you can directly get it from

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();

Upvotes: 0

Related Questions