Read inbox messages of a particular number and display them in an activity

后端 未结 3 1818
一整个雨季
一整个雨季 2020-12-05 09:53

I am trying to read messages from an inbox and display them in an activity. My activity contains a button and listview. When I click on the button, all messages from the inb

相关标签:
3条回答
  • 2020-12-05 10:28

    If you want to read messages only from particular number you need to somehow filter your data.

    This example filter cursor and sort it.

          selection = "_id = " + 15;
          Cursor curSms = context.getContentResolver().query(smsUri, null, selection,   null, "date ASC");
    
    0 讨论(0)
  • 2020-12-05 10:30

    add an if statement in your loop to match the selected number

    String selectedNumber;
    // copy the selected number to selectedNumber
    do {
            if(cursor.getString( indexAddr ).equals(selectedNumber))
            {
               String str = "Sender: " + cursor.getString( indexAddr ) + "\n" +     cursor.getString( indexBody );
               smsList.add( str );
            }
        }
    while( cursor.moveToNext() );
    

    Here only the messages from number matching the selected number will be added to smsList. It is an old question but i hope it will be helpful for someone

    0 讨论(0)
  • 2020-12-05 10:41

    try this code you get sms for pertucular number:

    import java.util.ArrayList;
    
    import android.app.Activity;
    import android.content.ContentResolver;
    import android.content.Context;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.util.Log;
    
    public class MainActivity extends Activity {
    
        ArrayList<String> sms_id = new ArrayList<String>();
        ArrayList<String> sms_num = new ArrayList<String>();
        ArrayList<String> sms_Name = new ArrayList<String>();
        ArrayList<String> sms_dt = new ArrayList<String>();
        ArrayList<String> sms_body = new ArrayList<String>();
    
        // private ImageView imageView;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Uri myMessage = Uri.parse("content://sms/");
            ContentResolver cr = this.getContentResolver();
            Cursor c = cr.query(myMessage, new String[] { "_id", "address", "date",
                    "body", "read" }, "address = '+9180009'", null, null);
            startManagingCursor(c);
            getSmsLogs(c, MainActivity.this);
    
        }
    
        public void getSmsLogs(Cursor c, Context con) {
    
            if (sms_num.size() > 0) {
                sms_id.clear();
                sms_num.clear();
                sms_Name.clear();
                sms_body.clear();
                sms_dt.clear();
            }
            try {
                if (c.moveToFirst()) {
                    do {
                        if (c.getString(c.getColumnIndexOrThrow("address")) == null) {
                            c.moveToNext();
                            continue;
                        }
                        String Number = c.getString(
                                c.getColumnIndexOrThrow("address")).toString();
                        String _id = c.getString(c.getColumnIndexOrThrow("_id"))
                                .toString();
                        String dat = c.getString(c.getColumnIndexOrThrow("date"))
                                .toString();
                        String Body = c.getString(c.getColumnIndexOrThrow("body"))
                                .toString();
                        Log.e("Body-->", "" + Body);
                        sms_id.add(_id);
                        sms_num.add(Number);
                        sms_body.add(Body);
                    } while (c.moveToNext());
                }
                c.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    }
    

    check log for sms body: you have to use below permission

    <uses-permission android:name="android.permission.READ_SMS" />
    
    0 讨论(0)
提交回复
热议问题