6

我需要获取短信列表并像在股票应用程序或 Go sms pro 中一样显示它们。我正在使用以下代码:

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());

它适用于我的 Galaxy 选项卡(android 2.2),但在我的 s3(ICS)应用程序启动时崩溃。我不想解析彩信,所以我尝试使用

uriSms = Uri.parse("content://sms/conversations");

但它不适用于两种设备。我搜索了很多以找到解决方案,但一无所获。我只发现访问短信对话取决于安卓操作系统和设备。我的目的是制作支持每个 android 设备 2.2+ 的应用程序。在股票应用程序中,他们使用 Thread.CONTENT_URI 来获取短信列表作为对话,例如。

Threads.CONTENT_URI.buildUpon().appendQueryParameter("simple", "true").build();

但是类 Thread 没有提供源代码,我在互联网上找不到它。我该怎么做才能让我的应用程序在每个 android 设备 (2.2+) 上运行,就像 Handcent Sms 或 GO sms pro 一样。

4

2 回答 2

2

您的代码正在崩溃,因为您假设查询的表包含名为 的列address,并非所有 android 版本都将地址存储在对话中。要检查表的结构,您可以通过以下代码显示其列名和内容:

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();

一种可能的解决方法是使用thread_id作为参数来搜索地址,如下所示:

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();
于 2013-02-19T10:51:00.280 回答
1
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();
于 2015-08-15T11:40:23.037 回答