Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will the address field store multiple phone numbers when I send a sms to multiple phone numbers at one time?

Tags:

android

When I send a sms in Android mobile phone, I can select multiple contact (Please see A.png ), and the sms will be as one record.

Can the sms be query using "content://sms/" ? will the field "address" return multiple phone numbers?

Thanks!

A.png

enter image description here

    public static List<String> ListDeleteOld(Context myContext, SMSRange mSMSRange, int beforeDays ) {
        List<String> mListSmsID=new ArrayList<String>();

        Uri uri=PublicParFun.GetUriBySMSRange(mSMSRange);

        Date beforeDate=getCurrentBefore(beforeDays);
        String[] projection = new String[] {"_id","address"};
        Cursor cur = myContext.getContentResolver().query(uri, projection, "date<=" + beforeDate.getTime(), null,  "date desc");


        while(cur.moveToNext()){           
            String s=cur.getString(cur.getColumnIndex("address"));           
        }
        cur.close();

        return mListSmsID;
    }



 public static Uri GetUriBySMSRange(SMSRange mSMSRange){
        Uri uri=null;

        final String SMS_URI_ALL = "content://sms/";
        final String SMS_URI_INBOX = "content://sms/inbox";
        final String SMS_URI_SEND = "content://sms/sent";
        final String SMS_URI_OUTBOX = "content://sms/outbox";
        final String SMS_URI_DRAFT = "content://sms/draft";

        switch (mSMSRange){
            case All:
                uri = Uri.parse(SMS_URI_ALL);
                break;

            case Inbox:
                uri = Uri.parse(SMS_URI_INBOX);
                break;

            case Sentbox:
                uri = Uri.parse(SMS_URI_SEND);
                break;

            case Outbox:
                uri = Uri.parse(SMS_URI_OUTBOX);
                break;

            case Draft:
                uri = Uri.parse(SMS_URI_DRAFT);
                break;
        }

        return uri;
    }
like image 948
HelloCW Avatar asked Jul 02 '15 03:07

HelloCW


1 Answers

set variables for example:

private static final Uri SMS_SENT_URI = Uri.parse("content://sms/sent");
private static final String SMS_ORDER = "date DESC";
private static final String ADDRESS_COLUMN_NAME = "address";
private static final String DATE_COLUMN_NAME = "date";
private static final String BODY_COLUMN_NAME = "body";
private static final String TYPE_COLUMN_NAME = "type";
private static final String ID_COLUMN_NAME = "_id";
private static final String SMS_PREFERENCES = "SMS_PREFERENCES";

You have to crete SmsObject. Then just loop trough Cursor.

The whole code: (call checkNewOutgoingSms(context))

 private void checkNewOutgoingSms(Context context) {

    Cursor smsCursor = getSmsCursor(context);
    List<Sms> smsList = getLastSmsList(smsCursor,context);
    if (smsList != null && smsList.size() > 0) {
        ProtectorWSODao mwtProtectorDao = new ProtectorWSODao();
        for (Sms sms : smsList) {
            //
            //
            //read sms content
            //
            //
        }
        Manager.getInstance(context).sendDataToServer(mwtProtectorDao);
    }
    smsCursor.close();
}

public static Cursor getSmsCursor(Context context) {
    return context.getContentResolver().query(SMS_SENT_URI, null, null, null, SMS_ORDER);
}

private List<Sms> getLastSmsList(Cursor smsCursor, Context context) {
    List<Sms> smsList = new ArrayList<Sms>();
    final int lastSmsIntercepted = smsStorage.getLastSmsIntercepted();
    boolean update = false;

    if (smsCursor != null) {
        if (smsCursor.moveToFirst()) {
            do {
                Sms smsParsed = parseSms(smsCursor, context);
                smsList.add(smsParsed);


            } while (smsCursor.moveToNext());
        }
    }
    return smsList;
}

public static Sms parseSms(Cursor cursor, Context context) {
    String number = cursor.getString(cursor.getColumnIndex(ADDRESS_COLUMN_NAME));
    String date = cursor.getString(cursor.getColumnIndex(DATE_COLUMN_NAME));
    String content = cursor.getString(cursor.getColumnIndex(BODY_COLUMN_NAME));
    int smsId = cursor.getInt(cursor.getColumnIndex(ID_COLUMN_NAME));

    return new Sms(Sms.SEND, smsId, number, date, content);
}
like image 97
5er Avatar answered Nov 13 '22 06:11

5er