我需要获取短信列表并将其显示在库存应用程序或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());
Run Code Online (Sandbox Code Playgroud)
它适用于我的galaxy选项卡(android 2.2),但在我的s3(ICS)应用程序启动时崩溃.我不想解析mms所以我尝试使用
uriSms = Uri.parse("content://sms/conversations");
Run Code Online (Sandbox Code Playgroud)
但它并不适用于这两种设备.我google了很多寻找解决方案,但我什么都没发现.我只发现对sms对话的访问取决于android os和device.我的目的是制作支持每个Android设备2.2+的应用程序.在库存应用程序中,他们使用Thread.CONTENT_URI获取短信列表作为对话,例如.
Threads.CONTENT_URI.buildUpon().appendQueryParameter("simple", "true").build();
Run Code Online (Sandbox Code Playgroud)
但类Thread没有提供源代码,我在互联网上找不到它.我可以做些什么来使我的应用程序在每个Android设备(2.2+)上运行,就像Handcent Sms或GO sms pro一样.
您的代码崩溃是因为您假设查询的表包含名为 的列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();
Run Code Online (Sandbox Code Playgroud)
一种可能的解决方法是使用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();
Run Code Online (Sandbox Code Playgroud)