According to the 4.4 SMS APIs, the new version provides functionality to:
allow apps to read and write SMS and MMS messages on the device
I can't find any information on this functionality, nor any samples in the new SDK. This is what I have so far for reading new incoming messages.
However, I would like to read existing messages stored on the deivce:
// Can I only listen for incoming SMS, or can I read existing stored SMS?
SmsMessage[] smsList = Telephony.Sms.Intents.getMessagesFromIntent(intent);
for(SmsMessage sms : smsList) {
Log.d("Test", sms.getMessageBody())
}
Please note: I know about using the SMS Content Provider, however this method is unsupported. According to the linked APIs, I should be able to do this in a supported way.
SMS (Short Message Service), commonly referred to as "text messaging," is a service for sending short messages of up to 160 characters (224 character limit if using a 5-bit mode) to mobile devices, including cellular phones and smartphones. Eye on Tech.
Messages by Google enables you to view your Android text messages through any device which uses an internet browser, and even send text messages using that device! For this method, you'll need the "Messages" by Google app on your Android device.
In some versions of Android, this permission is turned on by default. In other versions, this permission is turned off by default. To set the app's permission on a device or emulator instance, choose Settings > Apps > SMS Messaging > Permissions, and turn on the SMS permission for the app.
It looks like you would be able to use this class to get it working. The package is Telephony.Sms.Conversations.
Although the following code uses the content provider method, this is now an official API added in API Level 19 (KitKat) for reading the SMS messages.
public List<String> getAllSmsFromProvider() {
List<String> lstSms = new ArrayList<String>();
ContentResolver cr = mActivity.getContentResolver();
Cursor c = cr.query(Telephony.Sms.Inbox.CONTENT_URI, // Official CONTENT_URI from docs
new String[] { Telephony.Sms.Inbox.BODY }, // Select body text
null,
null,
Telephony.Sms.Inbox.DEFAULT_SORT_ORDER); // Default sort order
int totalSMS = c.getCount();
if (c.moveToFirst()) {
for (int i = 0; i < totalSMS; i++) {
lstSms.add(c.getString(0));
c.moveToNext();
}
} else {
throw new RuntimeException("You have no SMS in Inbox");
}
c.close();
return lstSms;
}
I did it like the following. Create SMSObject:
public class SMSObject {
private String _id;
private String _address;
private String _msg;
private String _readState; // "0" for have not read sms and "1" for have
// read sms
private String _time;
private String _folderName;
//+ getter and setter methods and
@Override
public String toString() {
return "SMSObject [_id=" + _id + ", _address=" + _address + ", _msg="
+ _msg + ", _readState=" + _readState + ", _time=" + _time
+ ", _folderName=" + _folderName + "]";
}
And here a simple function, which simply logs all current SMS-Objects
private void readSMS() {
List<SMSObject> lstSms = new ArrayList<SMSObject>();
SMSObject objSms = new SMSObject();
Uri message = Uri.parse("content://sms/");
ContentResolver cr = this.getContentResolver();
Cursor c = cr.query(message, null, null, null, null);
// this.startManagingCursor(c);
int totalSMS = c.getCount();
Log.d("SMS Count->", "" + totalSMS);
if (c.moveToFirst()) {
for (int i = 0; i < totalSMS; i++) {
objSms = new SMSObject();
objSms.setId(c.getString(c.getColumnIndexOrThrow("_id")));
objSms.setAddress(c.getString(c
.getColumnIndexOrThrow("address")));
objSms.setMsg(c.getString(c.getColumnIndexOrThrow("body")));
objSms.setReadState(c.getString(c.getColumnIndex("read")));
objSms.setTime(c.getString(c.getColumnIndexOrThrow("date")));
if (c.getString(c.getColumnIndexOrThrow("type")).contains("1")) {
objSms.setFolderName("inbox");
} else {
objSms.setFolderName("sent");
}
lstSms.add(objSms);
Log.d("SMS at " + i, objSms.toString());
c.moveToNext();
}
}
// else {
// throw new RuntimeException("You have no SMS");
// }
c.close();
// return lstSms;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With