gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:39:44 25 4
gpt4 key购买 nike

我需要获取短信列表并像在股票应用程序或 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com