PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 12, 2022

[FIXED] How to read all SMS from inbox by more than one sender but not by all sender in android?

 July 12, 2022     android, java, message, sms, telephonymanager     No comments   

Issue

I have one arraylist which have some contact number and i want to pass that arraylist to address. So that it would get all the messages from selected numbers which are in arraylist. Thank you in advance.

  StringBuilder smsBuilder = new StringBuilder();
    final String SMS_URI_INBOX = "content://sms/inbox";
    final String SMS_URI_ALL = "content://sms/";

    Uri uri = Uri.parse(SMS_URI_INBOX);
    String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };

    Cursor cur = getContentResolver().query(uri, projection, "address='"+list+"'" , null, "date desc");

    if (cur.moveToFirst())
    {
        int index_Address = cur.getColumnIndex("address");
        int index_Person = cur.getColumnIndex("person");
        int index_Body = cur.getColumnIndex("body");
        int index_Date = cur.getColumnIndex("date");
        int index_Type = cur.getColumnIndex("type");
        do
        {
            String strAddress = cur.getString(index_Address);
            int intPerson = cur.getInt(index_Person);
            String strbody = cur.getString(index_Body);
            long longDate = cur.getLong(index_Date);
            int int_Type = cur.getInt(index_Type);


            String str = "SMS From: " + cur.getString(index_Address) +
                    "\n" + cur.getString(index_Body) + "\n";
            arrayAdapter.add(str);

            smsBuilder.append("[ ");
            smsBuilder.append(strAddress + ", ");
            smsBuilder.append(intPerson + ", ");
            smsBuilder.append(strbody + ", ");
            smsBuilder.append(longDate + ", ");
            smsBuilder.append(int_Type);
            smsBuilder.append(" ]\n\n");
        } while (cur.moveToNext());

        if (!cur.isClosed())
        {
            cur.close();
            cur = null;
        }
    }
    else
    {
        smsBuilder.append("no result!");
    } // end if
}

Solution

       int argcount = MOBILENUMBER.size(); // number of IN arguments i.e More than one Mobile number for which you want read SMS

    //  String[] args = new String[]{ 1, 2 };
    StringBuilder inList = new StringBuilder(argcount * list.length);
    for (int i = 0; i < argcount; i++)
    {
        if(i > 0)
        {
            inList.append(",");
        }
        inList.append("?");//This will generate Number of "? " to match our item in array list
    }
    Cursor cur = getContentResolver().query(uri, projection, "address IN (" + inList.toString() + ")" , list, "date DESC ");


Answered By - Anjali Patel
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing