Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

xamarin android read sms Inbox messages

$
0
0

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
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.read_smsa" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="28" />
    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"></application>
  <uses-permission android:name="android.permission.READ_SMS" />
  <uses-permission android:name="android.permission.WRITE_SMS" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    </manifest>

      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());

            }
        }

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>