【问题标题】:read sms from emulator从模拟器读取短信
【发布时间】:2012-11-18 20:09:42
【问题描述】:

无法从模拟器读取短信。我需要能够将短信的“正文”转换为字符串以显示在 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();}

【问题讨论】:

    标签: java android eclipse android-emulator


    【解决方案1】:

    尝试使用startManagingCursor:

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

    【讨论】:

    • 我试过这个,但是通过这个方法立即画了一条线并且错误消息:该方法已被'弃用为“onCreate”。
    • 另外我要提一下,我已经在 AndroidManifest.xml 文件中添加了 READ_SMS 权限。
    • 已弃用意味着它仍在工作,但有更好的实现(CusorLoader)。该方法在您的 Activity 中吗?
    • 你可以用 cursor.open() 替换它,最后你必须使用 cursor.close()
    • 方法在活动内部。读取短信的最新方法是:使用 SMSManage.createFromPdu() 读取消息,而不是使用 : cursor = getContentResolver().query(Content_uri, null,null,null,null) ????
    猜你喜欢
    • 2017-03-05
    • 2023-03-05
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多