android - 如何在每个 Android 设备 2.2+ 中获取短信列表作为对话

标签 android list sms

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

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 操作系统和设备。我的目的是制作支持每个 android 设备 2.2+ 的应用程序。在库存应用程序中,他们使用 Thread.CONTENT_URI 获取短信列表作为对话,例如。

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

但是 Thread 类没有提供源代码,我在互联网上找不到它。 我该怎么做才能让我的应用程序像 Handcent Sms 或 GO sms pro 一样在每个 android 设备 (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();

关于android - 如何在每个 Android 设备 2.2+ 中获取短信列表作为对话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13210420/

相关文章:

android - 协程中的自定义拦截器

list - NoSuchMethodError:将值添加到列表时,在空值上调用了 'add'方法Flutter

python - 在同一个循环中迭代两个列表

Android – 监听传入的 SMS 消息

java - 我的 DiffUtil 实现有什么问题?

android - 为 Android 按钮属性分配负值

java - 安卓查看错误

python - “NoneType”对象没有属性 'insert' Python 列表 append 插入

java - 我们如何管理 SMS,哪种语言是最好的处理方式?

android - 启动应用程序时获取新短信的更快/最快的方法 - Android