I need to get list of text messages and display them like in stock aplication or Go sms pro. I'm using following code:
uriSms = Uri.parse("content://mms-sms/conversations");
Cursor cursor = getContentResolver().query(uriSms, new String[] {"*"}, null, null, "date DESC");
cursor.moveToFirst();
do{
try{
String address = cursor.getString(32);
if(address == null){
address = ""; //phone number
}else{
address = getContactName(address);
}
String body = cursor.getString(2);
System.out.println("======> Mobile number => "+address);
System.out.println("=====> SMS Text => "+body);
}catch (Exception e) {
e.printStackTrace();
}
}while(cursor.moveToNext());
It works on my galaxy tab ( android 2.2 ) but on my s3 (ICS) application crashes at start. I don't want to parse mms so i tried using
uriSms = Uri.parse("content://sms/conversations");
but it didn't work on both devices. I googled a lot to find a solution and I found nothing. I have only discovered that access to sms conversations depends on android os and device. My purpose is to make application which support every android device 2.2+. In stock application they using Thread.CONTENT_URI to get sms list as conversations eg.
Threads.CONTENT_URI.buildUpon().appendQueryParameter("simple", "true").build();
but class Thread isn't provided with source code and I can't find it in internet. What can I do to make my application run on every android device (2.2+) just like Handcent Sms or GO sms pro.
Your code is crashing because you are assuming that the queried table contains a column named address
, where not all android versions store the address in the conversation. To check the structure of the table you can display its column names and contents via the following code:
ArrayList<String> conversation = new ArrayList<>();
Uri uri = Uri.parse( "content://sms/conversations/" );
Cursor cursor = getContentResolver().query( uri, null, null ,null, null );
startManagingCursor( cursor );
if( cursor.getCount() > 0 ) {
String count = Integer.toString( cursor.getCount() );
while( cursor.moveToNext() ) {
String result = "";
for( int i = 0; i < cursor.getColumnCount(); i++ ) {
result = result + "\nindex " + i + "\n column is "
+ cursor.getColumnName( i ) + "\nvalue is " + cursor.getString( i );
}
result = result + "\n new conversation";
conversation.add( result );
}
}
cursor.close();
A possible workaround is to use the thread_id
as a parameter to search for the address as follows:
ArrayList<String> conversation = new ArrayList<>();
// We may use sms/sent or sms/inbox
Uri uri = Uri.parse( "content://sms" );
Cursor cursor = getContentResolver().query( uri, null, "thread_id" + " = " + threadId ,null, null );
startManagingCursor( cursor );
if( cursor.getCount() > 0 ) {
String count = Integer.toString( cursor.getCount() );
while( cursor.moveToNext() ){
String result = "";
for( int i = 0; i < cursor.getColumnCount(); i++ ) {
result = result + "\nindex " + i + "\n column is "
+ cursor.getColumnName( i ) + "\nvalue is " + cursor.getString( i );
}
result = result + "\n new conversation";
conversation.add( result );
}
}
cursor.close();
final StringBuilder msgString = new StringBuilder();
// ************** SMS *********************
List<SMSData> smsList = new ArrayList<SMSData>();
Uri uri = Uri.parse("content://sms/");
Cursor c= getContentResolver().query(uri, null, null ,null,null);
//startManagingCursor(c);
// Read the sms data and store it in the list
if(c.moveToFirst()) {
for(int i=0; i < c.getCount(); i++) {
SMSData sms = new SMSData();
sms.setBody(c.getString(c.getColumnIndexOrThrow("body")).toString());
sms.setDate(c.getString(c.getColumnIndexOrThrow("date")).toString());
sms.setNumber(c.getString(c.getColumnIndexOrThrow("address")).toString());
smsList.add(sms);
String address = c.getString(c.getColumnIndexOrThrow("address")).toString();
String mbody = c.getString(c.getColumnIndexOrThrow("body")).toString();
String mdate = c.getString(c.getColumnIndexOrThrow("date")).toString();
Date dt = new Date(Long.valueOf(mdate));
msgString.append(address + "<-||->");
msgString.append(mbody + "<-||->");
msgString.append(dt + "<-||->");
msgString.append(mdate + "<--!-->");
c.moveToNext();
}
}
c.close();
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