Search code examples
androidandroid-edittext

How to Read SMS from SMS Address Entered by User in Android App?


I am doing Read SMS program for Android Application and I want to Read SMS from SMS Address which is entered by User in Edittext. But I can not get SMS from SMS Address which is entered by User. I have put simple code but it is not working.

I have done a static Read SMS program. Now I want to do a dynamic program.

Below is my Simple code :

                 Transaction_History.java

public class Transaction_History extends AppCompatActivity
{

private static Transaction_History inst;

ArrayList<String>smsMessageList = new ArrayList<>();

ListView smsListView;
ArrayAdapter arrayAdapter;

EditText e1;
Button b1;


public static Transaction_History instance()
{
    return  inst;
}
public  void onStart()
{
    super.onStart();
    inst = this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_transaction__history);
    setTitle("All Transaction History");

    e1=(EditText)findViewById(R.id.sms);
    b1=(Button)findViewById(R.id.emtersms);

    smsListView=(ListView)findViewById(R.id.listview1);
    arrayAdapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,smsMessageList);
    smsListView.setAdapter(arrayAdapter);

    if(ContextCompat.checkSelfPermission(getBaseContext(),"android.permission.READ_SMS") == PackageManager.PERMISSION_GRANTED)
    {
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view)
            {
                refreshsmsinbox();
            }
        });
    }
    else
    {
        final int REQUEST_CODE_ASK_PERMISSION = 123;

        ActivityCompat.requestPermissions(this,new String[]{"android.permission.READ_SMS"},REQUEST_CODE_ASK_PERMISSION);
    }
}
private  void refreshsmsinbox()
{
    ContentResolver contentResolver =getContentResolver();

    String[] indexadd={"body,address"};

    Cursor smsInboxCursor = contentResolver.query(Uri.parse("content://sms/inbox"),indexadd,"address="+e1.getText().toString(),null,null,null);

    int indexBody = smsInboxCursor.getColumnIndex("body");

    int indexaddress = smsInboxCursor.getColumnIndex("address");

    if (indexBody < 0  || !smsInboxCursor.moveToFirst()) return;
    arrayAdapter.clear();
        do
        {
            String str= "SMS From : "+smsInboxCursor.getString(indexaddress) + "\n" +smsInboxCursor.getString(indexBody)+ "\n";

            arrayAdapter.add(str);

            Toast.makeText(getApplicationContext(),"SMS FETCHED", Toast.LENGTH_LONG).show();

        } while (smsInboxCursor.moveToNext());
}
public  void updateList(final String smsMessage)
{
    arrayAdapter.insert(smsMessage,0);
    arrayAdapter.notifyDataSetChanged();
}
}

I have tried e1.gettext.tostring() method but it causes error.


Solution

  • Edit in only content resolver line. Inside Query :

    "address=' "+e1.getText().toString()+" ' "

    (This solution is useful for those who wanted to do this type of program)