BroadcastReceiver不处理收到的短信

时间:2014-08-15 21:45:55

标签: android sms broadcastreceiver

我想更新总消息,即收到新短信时的最后一个短信和最后一个短信的计数和日期。但它没有发生,只有当我重新启动活动时才会发生。

     getAll.clear();
        Uri SMS_INBOX = Uri.parse("content://mms-sms/conversations?simple=true");

         c = getContentResolver().query(SMS_INBOX, null, null, null, "date DESC");

            startManagingCursor(c);
            count = new String[c.getCount()];
            snippet = new String[c.getCount()];
          thread_id = new String[c.getCount()];

            String[] num=new String[c.getCount()];
         date=new String[c.getCount()];
         address=new String[c.getCount()];
          c.moveToFirst();
            for (int i = 0; i < c.getCount(); i++) 
            {
                count[i] = c.getString(c.getColumnIndexOrThrow("message_count"))
                        .toString();
                thread_id[i] = c.getString(c.getColumnIndexOrThrow("_id"))
                        .toString();
                snippet[i] = c.getString(c.getColumnIndexOrThrow("snippet"))
                        .toString();
                date[i]=c.getString(c.getColumnIndexOrThrow("date"))
                        .toString();

                cur=getContentResolver().query(Uri.parse("content://sms/"), null, "thread_id = " + thread_id[i], null, null);
                startManagingCursor(cur);
                cur.moveToFirst();
                     num[i]=getContactName(this,cur.getString(cur.getColumnIndexOrThrow("address")).toString());
               address[i]=cur.getString(cur.getColumnIndexOrThrow("address")).toString();
               getAll.add(count[i]+","+num[i]+","+snippet[i]);
               cur.moveToNext();

              c.moveToNext();

            }
            adapter = new HomeAdapter(this, R.layout.sms, getAll,date);

            adapter.notifyDataSetChanged();
            list.setAdapter(adapter);

BroadCast Reciever

        if (null != bndl)
              {
     //---retrieve the SMS message received---
                  Object[] pdus = (Object[]) bndl.get("pdus");

                  msg = new SmsMessage[pdus.length];  

                  for (int i=0; i<msg.length; i++){
                      msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]);            
                      contentTitle=msg[i].getOriginatingAddress().toString();
                      //contentTitle = (String) DateFormat.format("dd/MM/yy:hh:mm a", new Date(msg[i].getTimestampMillis()));
                      contentText = msg[i].getMessageBody().toString();

                        }
                  }

我尝试了所有方法,但未能更新,不知道现在该做什么。我从6小时开始坚持这个问题。

1 个答案:

答案 0 :(得分:0)

这应该有效吗

void update() {
    getAll.clear();
    Uri SMS_INBOX = Uri.parse("content://mms-sms/conversations?simple=true");
    Cursor c = getContentResolver().query(SMS_INBOX, null, null, null, "date DESC");

    startManagingCursor(c);
    count = new String[c.getCount()];
    snippet = new String[c.getCount()];
    thread_id = new String[c.getCount()];

    String[] num = new String[c.getCount()];
    date = new String[c.getCount()];
    address = new String[c.getCount()];
    c.moveToFirst();

    try {
        int i = 1;
        if (c.getCount() >= 1) {
            while (c.moveToNext()) {
                if (i == 35) {
                    i = 35;
                }

                count[i] = c.getString(c.getColumnIndexOrThrow("message_count")).toString();
                thread_id[i] = c.getString(c.getColumnIndexOrThrow("_id")).toString();
                snippet[i] = c.getString(c.getColumnIndexOrThrow("snippet"));
                date[i] = c.getString(c.getColumnIndexOrThrow("date"));
                Cursor cur = getContentResolver().query(Uri.parse("content://sms/"), null, "thread_id = " + thread_id[i], null, null);
                cur.moveToFirst();
                startManagingCursor(cur);
                try {
                    num[i] = getContactName(this, cur.getString(cur.getColumnIndexOrThrow("address")).toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    address[i] = cur.getString(cur.getColumnIndexOrThrow("address")).toString();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                getAll.add(count[i] + "," + num[i] + "," + snippet[i]);
                i++;
            }

        }

    } catch (Exception e)

    {
        e.printStackTrace();
    }
    adapter = new HomeAdapter(this, R.layout.sms, getAll, date);

    adapter.notifyDataSetChanged();
    list.setAdapter(adapter);

}