Created
October 16, 2017 05:33
-
-
Save wangerekaharun/a118a4151aae4e4f2b516c8b4dc99731 to your computer and use it in GitHub Desktop.
Code for reading SMS in android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//add permission Read SMS in android manifest to enable this code to work | |
StringBuilder smsBuilder = new StringBuilder(); | |
final String SMS_URI_INBOX = "content://sms/inbox"; | |
final String SMS_URI_ALL = "content://sms/"; | |
try { | |
Uri uri = Uri.parse(SMS_URI_ALL); | |
String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" }; | |
//retrieving sms for a particular number,you can set to null so as to read all sms in inbox folder | |
Cursor cur = getContentResolver().query(uri, projection, "address= '254712345678'", null, "date desc"); | |
if (cur.moveToFirst()) { | |
int index_Address = cur.getColumnIndex("address"); | |
int index_Person = cur.getColumnIndex("person"); | |
int index_Body = cur.getColumnIndex("body"); | |
int index_Date = cur.getColumnIndex("date"); | |
int index_Type = cur.getColumnIndex("type"); | |
do { | |
String strAddress = cur.getString(index_Address); | |
int intPerson = cur.getInt(index_Person); | |
String strbody = cur.getString(index_Body); | |
long longDate = cur.getLong(index_Date); | |
int int_Type = cur.getInt(index_Type); | |
smsBuilder.append("[ "); | |
smsBuilder.append(strAddress + ", "); | |
smsBuilder.append(intPerson + ", "); | |
smsBuilder.append(strbody + ", "); | |
smsBuilder.append(longDate + ", "); | |
smsBuilder.append(int_Type); | |
smsBuilder.append(" ]\n\n"); | |
//setting the sms in a textview(smsText) | |
smsText.setText(smsBuilder); | |
} while (cur.moveToNext()); | |
if (!cur.isClosed()) { | |
cur.close(); | |
cur = null; | |
} | |
} else { | |
smsBuilder.append("no result!"); | |
} // end if | |
} catch (SQLiteException ex) { | |
Log.d("SQLiteException", ex.getMessage()); | |
} | |
} | |
//read sms list | |
public List<String> getAllSmsFromProvider() { | |
List<String> lstSms = new ArrayList<String>(); | |
Uri smsUri = Uri.parse("content://sms/inbox"); | |
Cursor cursor = getContentResolver().query(smsUri, null, null, null, null); | |
while (cursor.moveToNext()) { | |
String body = cursor.getString(cursor.getColumnIndex("body")); | |
lstSms.add(body); | |
} | |
return lstSms; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how can set sms who recieved from one address and show that one or more message in recycleview