Read all SMS from a particular sender

前端 未结 5 1204
傲寒
傲寒 2020-11-30 22:53

How do I read all SMSes from a particular sender to me? E.g. I want to scan a) the body, and b) the date/time of all SMSes that came from \'TM-MYAMEX\' to the phone.

5条回答
  •  渐次进展
    2020-11-30 23:02

     protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            inbox = (Button) findViewById(R.id.inbox);
            list = (ListView) findViewById(R.id.list);
            arlist = new ArrayList();
            inbox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Uri inboxUri = Uri.parse("content://sms/inbox");
                    String[] reqCols = {"_id", "body", "address"};
                    ContentResolver cr = getContentResolver();
                    Cursor cursor = cr.query(inboxUri, reqCols, "address='+919456'", null, null);
                    adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.msg_content_layout, cursor,
                            new String[]{"body", "address"}, new int[]{R.id.txt1, R.id.txt2});
                    list.setAdapter(adapter);
                }
            });
    
        }
    

    Here I have added the number +919456 as the value of the address field.

    Apart from this you need to add the READ_SMS permission to manifest:

提交回复
热议问题