How to Get text messages today
The first step is to apply for permissions in AndroidMainfest:
The following is the code for getting a text message:
// Set all text messages
Public void getMobileMessage (Intent m ){
Final String SMS_URI_ALL = "content: // sms/"; // all text messages
Final String SMS_URI_INBOX = "content: // sms/inbox"; // No text message is sent.
Final String SMS_URI_SEND = "content: // sms/sent"; // you have sent an sms.
Final String SMS_URI_DRAFT = "content: // sms/draft"; // draft box
Int count = 0;
StringBuilder smsBuilder = new StringBuilder ();
Try {
ContentResolver cr = m. getContentResolver ();
String [] projection = new String [] {"_ id", "address", "person ",
"Body", "date", "type "};
Uri uri = Uri. parse (SMS_URI_ALL );
Cursor cur = cr. query (uri, projection, null, null, "date desc ");
If (cur. moveToFirst ()){
String name;
String phoneNumber;
String smsbody;
String date;
String type;
Int nameColumn = cur. getColumnIndex ("person ");
Int phoneNumberColumn = cur. getColumnIndex ("address ");
Int smsbodyColumn = cur. getColumnIndex ("body ");
Int dateColumn = cur. getColumnIndex ("date ");
Int typeColumn = cur. getColumnIndex ("type ");
Do {
Name = cur. getString (nameColumn );
PhoneNumber = cur. getString (phoneNumberColumn );
Smsbody = cur. getString (smsbodyColumn );
SimpleDateFormat dateFormat = new SimpleDateFormat (
"Yyyy-MM-dd hh: mm: ss ");
Date d = new Date (Long. parseLong (cur. getString (dateColumn )));
Date = dateFormat. format (d );
Int typeId = cur. getInt (typeColumn );
If (typeId = 1 ){
Type = "receive ";
} Else if (typeId = 2 ){
Type = "send ";
} Else {
Type = "";
}
SmsBuilder. append ("[");
SmsBuilder. append (name + ",");
SmsBuilder. append (phoneNumber + ",");
SmsBuilder. append (smsbody + ",");
SmsBuilder. append (date + ",");
SmsBuilder. append (type );
SmsBuilder. append ("]");
If (smsbody = null)
Smsbody = "";
Count ++;
} While (cur. moveToNext () & count <10); // read the first 10 messages sorted by default.
} Else {
SmsBuilder. append ("no result! ");
}
SmsBuilder. append ("getSmsInPhone has executed! ");
} Catch (SQLiteException ex ){
Log. d ("SQLiteException in getSmsInPhone", ex. getMessage ());
}
If you have reprinted the results of your hard work, please indicate the source. Thank you!