Arslan
Arslan

Reputation: 349

android: push sms As recieved in inbox using getContentResolver

on sms received , i have saved that sms in my database now i want to move that sms into inbox

i used this code but it move it as sent by me please help me to move it as a received sms

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);

Upvotes: 0

Views: 561

Answers (3)

Raymond Xie
Raymond Xie

Reputation: 1506

Change:

cv2.put("type", 2);

To:

cv2.put("type", 1);

Because:

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

Upvotes: 1

Rahul
Rahul

Reputation: 479

Use thisvalues.put("status", SmsManager.STATUS_ON_ICC_UNREAD); . Status can be anything like read/unread/seen. I have keep it as unread.

Look at Message status

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

and

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);
}

}

Upvotes: 0

B M
B M

Reputation: 1882

You can use following method for deleting SMS from Inbox,

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();
    }
} 

}

Upvotes: 0

Related Questions