【问题标题】:How to read message from inbox如何阅读收件箱中的邮件
【发布时间】:2012-02-23 06:16:51
【问题描述】:

这是我在上下文菜单中按下查看按钮时用来阅读收件箱消息的代码,但我无法阅读消息,我只得到第一条消息的正文。

 public class Smsread extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  TextView view = new TextView(this);
  Uri uri = Uri.parse("content://sms/inbox");
  Cursor c= getContentResolver().query(uri, null, null ,null,null);
  startManagingCursor(c);
  String sms = "";
  if(c.moveToFirst()){
      for(int i=0;i<c.getCount();i++){
   sms= c.getString(c.getColumnIndexOrThrow("body")).toString();
  // sms=c.getString(c.getColumnIndexOrThrow("address")).toString();

      }
      c.close();
      c.moveToNext();

      /*
  sms ="From :" + c.getString(2) + " : " + c.getString(11)+"\n";         
  */
  }

  view.setText(sms);
  setContentView(view);

}
}              

【问题讨论】:

    标签: android sms provider inbox


    【解决方案1】:

    试试这个:

    if(c.moveToFirst()){
    
          for(int i=0;i<c.getCount();i++){
    
             sms= c.getString(c.getColumnIndexOrThrow("body")).toString();
             c.moveToNext();
          }
    }
    

    对于 for 循环的每次迭代,您都必须移动到下一条记录。然后,您将在那里获得所有消息。在这里,在 for 循环中,在您获得 String sms 后,您可以使用该 sms 显示在某个地方。每次,它都会抓取下一条短信!

    希望你明白了!

    【讨论】:

    • @parag:谢谢 parag,你说的逻辑是正确的,但我尝试在我的程序中应用相同的逻辑,但是当我为每条消息按下查看按钮时,我得到的第一条消息的正文相同。
    • @Hiral :我试过了兄弟,但它不起作用同样的问题仍在继续。请帮帮我..
    • @dippu:但是您在使用我的代码行时遇到了什么问题?我希望您在清单文件中包含了所需的权限。
    • @Hiral:实际上我已经使用 bundle 将我的消息内容从一个活动移动到另一个活动。我有一个 readsms.java 文件,它在单击时读取 sms 的内容。我面临的问题在于列表视图我得到了正确的数字和相应的内容。但是当我点击那个特定的列表项来阅读内容时,我得到了一些其他消息的内容。光标有问题。我无法弄清楚。
    • @dippu:请在设置列表视图的位置发布完整代码并覆盖其 onItemClickListener。同时发布您的自定义适配器类。
    【解决方案2】:

    你必须改变

     sms= c.getString(c.getColumnIndexOrThrow("body")).toString();
    

     sms+= c.getString(c.getColumnIndexOrThrow("body")).toString();
    

    【讨论】:

      【解决方案3】:

      试试这个:

      String[] sms=new String[c.getCount()];
      
      if(c.moveToFirst()){
      
            do{
               sms= c.getString(c.getColumnIndexOrThrow("body")).toString();
      
             } while(c.moveToNext());
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-01
        • 2014-05-20
        • 2011-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多