android:push sms使用getContentResolver在收件箱中收到

时间:2014-08-27 07:14:14

标签: android sms inbox

收到短信后,我已将该短信保存在我的数据库中 现在我想将短信移入收件箱

我使用了这段代码,但它按我发送的方式移动了它 请帮我把它作为收到的短信移动

ListViewLogItem lm = listArray.get(position);
                long datein = Long.parseLong(lm.getInboxTime());
                    Uri uri = Uri.parse("content://sms/");
                    ContentValues cv2 = new ContentValues();
                    cv2.put("address","+"+lm.getNumber());
                    cv2.put("date", datein);
                    cv2.put("read", 1);
                    cv2.put("type", 2);
                    cv2.put("body", lm.getSms());


                    getContentResolver().insert(Uri.parse("content://sms/inbox"), cv2);

3 个答案:

答案 0 :(得分:1)

变化:

cv2.put("type", 2);

要:

cv2.put("type", 1);

由于:

public static final int MESSAGE_TYPE_INBOX = 1;
public static final int MESSAGE_TYPE_SENT = 2;

答案 1 :(得分:0)

您可以使用以下方法从收件箱中删除短信,

private void deleteMessage()
{
Cursor c = getContentResolver().query(SMS_INBOX, null, null, null, null); 
//c.moveToFirst(); 

while (c.moveToNext())
{
    System.out.println("Inside if loop");

    try
    {
        String address = c.getString(2);
        String MobileNumber = mainmenu.getParameterData().getMobileNumber().trim();

        //Log.i( LOGTAG, MobileNumber + "," + address );

        Log.i( LOGTAG, c.getString(2) );


        if ( address.trim().equals( MobileNumber ) )
        {
            String pid = c.getString(1);
            String uri = "content://sms/conversations/" + pid;
            getContentResolver().delete(Uri.parse(uri), null, null);
            stopSelf();
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
} 

}

答案 2 :(得分:0)

使用此values.put("status", SmsManager.STATUS_ON_ICC_UNREAD);。状态可以是读/未读/看到的。我把它保持为未读。

查看消息状态

values.put("read", true);  // As Read

values.put("read", false); // As Un Read

public class Message {



final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
@SuppressWarnings("unused")
private ContentResolver resolver;

public Message(ContentResolver ConResolver){
    resolver = ConResolver;
}

public String getMessage(int batas) {
      Cursor cur = resolver.query(SMS_INBOX, null, null, null,null);
      String sms = "Message >> \n";
      int hitung = 0;
      while (cur.moveToNext()) {
          sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";
          if(hitung == batas)
              break;
          hitung++;
      }
      return sms;
}

public int getMessageCountUnread(){
    Cursor c = resolver.query(SMS_INBOX, null, "read = 0", null, null);
    int unreadMessagesCount = c.getCount();
    c.deactivate();
    return unreadMessagesCount;
}

public String getMessageAll(){
      Cursor cur = resolver.query(SMS_INBOX, null, null, null,null);
      String sms = "Message >> \n";
      while (cur.moveToNext()) {
          sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";
      }
      return sms;
}

public String getMessageUnread() {
    Cursor cur = resolver.query(SMS_INBOX, null, null, null,null);
      String sms = "Message >> \n";
      int hitung = 0;
      while (cur.moveToNext()) {
          sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";
          if(hitung == getMessageCountUnread())
              break;
          hitung++;
      }
      return sms;
}

public void setMessageStatusRead() {
    ContentValues values = new ContentValues();
    values.put("read",true);
    resolver.update(SMS_INBOX,values, "_id="+SmsMessageId, null);
}

}