java - 从模拟器读取短信

标签 java android eclipse android-emulator

无法从模拟器读取短信。我需要能够将短信的“正文”转换为字符串以显示在 TextView 或 EditView 中。我不是要使用“onRecieve”短信,而是要阅读短信。我可以通过 DDMS Devices Emulator Control(Windows7) 或使用 2 个模拟器发送到模拟器,即从端口 5554 发送到端口 5556。消息显示在模拟器中,但是当我尝试读取短信时,cursor.getCount( ) 只返回 O,即使“正文”(即短信文本)显示在模拟器上。我认为 cursor.getCount() 必须等于 1 或更大才能读取短信,但它仅 = 0。我该怎么做才能使模拟器显示 cursor.getCount() > 0,并允许我实际阅读短信正文?即如何使下面的代码工作。使用模拟器时,计数显示为 0,即使我已发送短信:

if(count>0){
   if(cursor.moveToFirst()){
    body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();
    txtLastSMS.setText(body);
   }
 }

相关代码如下:

    Uri CONTENT_URI = Uri.parse("content://sms/sent");    
    Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null,     null);                                   
    String body = null;
    if (cursor != null)
       try
       {
          int count = cursor.getCount();
          if(count>0){
             if(cursor.moveToFirst()){
             body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();
             txtLastSMS.setText(body);
          }
        }   
      }
      finally{ cursor.close();}

最佳答案

尝试使用 startManagingCursor:

Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null,     null);
startManagingCursor(cursor);

关于java - 从模拟器读取短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13444097/

相关文章:

java.security 与 javax.security

java.sql.SQLIntegrityConstraintViolationException : Cannot add or update a child row: a foreign key constraint fails

Android Studio AdMob 错误无法解析符号 @integer/google_play_services_version"/>

android - 如何将库项目添加到 Android Studio?

java - 在 SVN 中处理 .project 文件和不同的 eclipse 设置?

java - 如何在已导入 Spring Web 项目的非 Web 项目中运行测试用例?

Java- 导出到 jar- I/O 问题

java - 使用 groovy 为 java 应用程序编写集成测试有什么好处?

android - NativeScript Http 发布请求被阻止

java - 如何在 Eclipse 中处理 Maven WAR 覆盖?