I am trying to print all SMS in Logcat , but its not printing SMS in Logcat , I added a toast to check the size of the list ,I m getting Toast which shows list size is 7662, Help ,I am following the second answer from this How can I read SMS messages from the device programmatically in Android?
I have added all permissions in Manifest,
what i have tried in My Mainactivity:-
AllsmsList = getAllSms();
Toast.makeText(this, "Message Data Size "+ AllsmsList.size(), Toast.LENGTH_SHORT).show();
for (int i = 1;i <100;i++){
Log.v("SMS",AllsmsList.get(i).getId()+AllsmsList.get(i).getTime()+AllsmsList.get(i).getMsg());
}
my method to read sms :-
public List<Sms> getAllSms() {
List<Sms> lstSms = new ArrayList<Sms>();
Sms objSms = new Sms();
Uri message = Uri.parse("content://sms/");
ContentResolver cr = MainActivity.this.getContentResolver();
Cursor c = cr.query(message, null, null, null, null);
MainActivity.this.startManagingCursor(c);
int totalSMS = c.getCount();
//String receiveDayTime = Functions.dateFromMilisec(Long.valueOf(c.getColumnIndexOrThrow("date")), "hh:mm a MMM dd, yyyy");
if (c.moveToFirst()) {
for (int i = 0; i < totalSMS; i++) {
objSms = new Sms();
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.v("SMS",objSms.getMsg());
c.moveToNext();
}
}
// else {
// throw new RuntimeException("You have no SMS");
// }
c.close();
return lstSms;
}
Sms class:-
public class Sms {
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;
public String getId(){
return _id;
}
public String getAddress(){
return _address;
}
public String getMsg(){
return _msg;
}
public String getReadState(){
return _readState;
}
public String getTime(){
return _time;
}
public String getFolderName(){
return _folderName;
}
public void setId(String id){
_id = id;
}
public void setAddress(String address){
_address = address;
}
public void setMsg(String msg){
_msg = msg;
}
public void setReadState(String readState){
_readState = readState;
}
public void setTime(String time){
_time = time;
}
public void setFolderName(String folderName){
_folderName = folderName;
}
}