Share via


xamarin android read sms Inbox messages

Question

Tuesday, March 24, 2020 8:30 PM

hi Use the following code to display sms in listview. The problem is that it only displays one message, it does not display all messages and does not work on Android devices above 5

``` AndroidManifest.xml

```

``` string[] items; public ListView ListView;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

        Button btn = FindViewById<Button>(Resource.Id.button1);
         btn.Click += Btn_Click;
        //

    }
    private void Btn_Click(object sender, System.EventArgs e)
    {
        try
        {
            getAllSms();
            ListView = (ListView)FindViewById<ListView>(Resource.Id.listView1);
            ListView.Adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, items);
        }
        catch
        {
        }


    }
    public void getAllSms()
    {

        string INBOX = "content://sms/inbox";
        string[] reqCols = new string[] { "_id", "thread_id", "address", "person", "date", "body", "type" };
        Android.Net.Uri uri = Android.Net.Uri.Parse(INBOX);
        var cursor = ContentResolver.Query(uri, reqCols, null, null, null);

        if (cursor.MoveToFirst())
        {
            do
            {
                String messageId = cursor.GetString(cursor.GetColumnIndex(reqCols[0]));
                String threadId = cursor.GetString(cursor.GetColumnIndex(reqCols[1]));
                String address = cursor.GetString(cursor.GetColumnIndex(reqCols[2]));
                String name = cursor.GetString(cursor.GetColumnIndex(reqCols[3]));
                String date = cursor.GetString(cursor.GetColumnIndex(reqCols[4]));
                String msg = cursor.GetString(cursor.GetColumnIndex(reqCols[5]));
                String type = cursor.GetString(cursor.GetColumnIndex(reqCols[6]));

                items = new string[] {
       ((messageId + (","
                        + (threadId + (","
                        + (address + (","
                        + (name + (","
                        + (date + (" ,"
                        + (msg + (" ," + type))))))))))))) };

            } while (cursor.MoveToNext());

        }
    }

```

All replies (8)

Wednesday, March 25, 2020 7:41 AM âś…Answered

I tested the code on my emulator (android 8.1), it works properly. The result is:

You can get the test demo in the attachment.

The inbox screenshot is:


Wednesday, March 25, 2020 2:39 AM

When you executed the following loop,you always created a new object new string[] {} to items,so the value of items always the latest message accessed(cursor.MoveToNext()).

 items = new string[] {
           ((messageId + (","
                            + (threadId + (","
                            + (address + (","
                            + (name + (","
                            + (date + (" ,"
                            + (msg + (" ," + type))))))))))))) };

You can define items as follows.

List<string> items = new List<string>();

instead of

 string[] items;

And when you get each item of sms, you can add this sms to items

 items.Add((messageId + (","
                        + (threadId + (","
                        + (address + (","
                        + (name + (","
                        + (date + (" ,"
                        + (msg + (" ," + type)))))))))))));

Note:

When we check the following function ,we will find the data parameter type is IList<T> objects,so we can also use ArrayAdapter normally.

   public ArrayAdapter(Context context, int textViewResourceId, IList<T> objects);

Wednesday, March 25, 2020 3:42 AM

hi jezh

In the beginning, I thank you for your response

The first issue has been resolved and I have a second problem, why does the code not work with versions 5 or higher?


Wednesday, March 25, 2020 5:23 AM

why does the code not work with versions 5 or higher?

What code are you talking about by the words the code? Could you please elaborate on your issue?

BTW, If you think my answer is correct or useful, could you please mark it as an answer?


Wednesday, March 25, 2020 5:37 AM

whay The code does not work on Android versions higher than 5 .


Wednesday, March 25, 2020 5:42 AM

Have you added the runtime permission? An app that targets Android 6.0 or higher must always perform a runtime permission check.


Wednesday, March 25, 2020 5:54 AM

code work don in android 4.4 and less . and does not work on Android versions higher than 5 .


Wednesday, March 25, 2020 3:11 PM

thanks jezh