Archive for code

Code for reading inbox sms in android

Tested on android 2.1 and 2.2.

Uri SMSURI = Uri.parse("content://sms/inbox");
String[] projection = new String[]{"_id", "address", "body", "date"};
Cursor cursor = null;
try {
	cursor = getContentResolver().query(SMSURI
			, projection
			, null //selection
			, null //selectionArgs
			, null); //sortOrder

	if (cursor != null && cursor.moveToFirst()) {
		do {
			int    id    = cursor.getInt(cursor.getColumnIndex("_id"));
			String address = cursor.getString(cursor.getColumnIndex("address"));
			String body = cursor.getString(cursor.getColumnIndex("body"));
			String date = cursor.getString(cursor.getColumnIndex("date"));

			SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy - HH:mm:ss");
			date = formatter.format(new Date(Long.parseLong(date))); 

			Log.d(Constants.LOGCAT, "id: " + id + "  address: " + address + "  body: " + body + "  date: " + date);

		} while (cursor.moveToNext());
	}
} finally {
	if (cursor != null) {
		cursor.close();
	}
}

Remember to set the properly permissions in android.manifest


<uses-permission android:name="android.permission.READ_SMS" />

Leave a Comment

Bad Behavior has blocked 3 access attempts in the last 7 days.