title: How to code read sms messages from android kitkat Delphi XE8
body: <p>I would appreciate some help in finding a working code for reading sms messages, on android kitkat. I am aware that the app has to be a default sms app with relevant permissions. I really don't want to annoy the user with any popup dialog requesting that the user manually selects the default sms app. I would like my app to perform as the default sms until its task is completed then return the setting to the original default sms that the user or factory has set, before using my app.</p>
  
  <p>Currently, I am NOT able to display any sms messages and can't see why. I am, however, seeing my app listed among the default sms apps on the testing device.</p>
  
  <p>Here is the code that I am using:</p>
  
  <pre class="lang-pascal prettyprint-override"><code>function TForm1.Process_SMS: string;
  var
    cursor: JCursor;
    uri: Jnet_Uri;
    address, person, msgdatesent, protocol, msgread, msgstatus, msgtype,
    msgreplypathpresent, subject, body,
    smsid, servicecenter, locked: string;
    msgunixtimestampms: int64;
    id_smsid, addressidx, personidx, msgdateidx, msgdatesentidx, protocolidx,
    msgreadidx, msgstatusidx, msgtypeidx, msgreplypathpresentidx, subjectidx,
    bodyidx, servicecenteridx, lockedidx: integer;
  begin
    uri := StrToJURI('content://sms/inbox');
    cursor := SharedActivity.getContentResolver.query(uri, nil, nil, nil, nil);
    id_smsid := cursor.getColumnIndex(StringToJstring('_id'));
    addressidx := cursor.getColumnIndex(StringToJstring('address'));
    personidx := cursor.getColumnIndex(StringToJstring('person'));
    msgdateidx := cursor.getColumnIndex(StringToJstring('date'));
    msgdatesentidx := cursor.getColumnIndex(StringToJstring('date_sent'));
    protocolidx := cursor.getColumnIndex(StringToJstring('protocol'));
    msgreadidx := cursor.getColumnIndex(StringToJstring('read'));
    msgstatusidx := cursor.getColumnIndex(StringToJstring('status'));
    msgtypeidx := cursor.getColumnIndex(StringToJstring('type'));
    msgreplypathpresentidx := cursor.getColumnIndex(StringToJstring('reply_path_present'));
    subjectidx := cursor.getColumnIndex(StringToJstring('subject'));
    bodyidx := cursor.getColumnIndex(StringToJstring('body'));
    servicecenteridx := cursor.getColumnIndex(StringToJstring('service_center'));
    lockedidx := cursor.getColumnIndex(StringToJstring('locked'));
  
    while (cursor.moveToNext) do
    begin
      smsid := JStringToString(cursor.getString(id_smsid));
      address := JStringToString(cursor.getString(addressidx));
      person := JStringToString(cursor.getString(personidx));
      msgunixtimestampms := cursor.getLong(msgdateidx);
      msgdatesent := JStringToString(cursor.getString(msgdatesentidx));
      protocol := JStringToString(cursor.getString(protocolidx));
      msgread := JStringToString(cursor.getString(msgreadidx));
      msgstatus := JStringToString(cursor.getString(msgstatusidx));
      msgtype := JStringToString(cursor.getString(msgtypeidx));
      msgreplypathpresent := JStringToString(cursor.getString(msgreplypathpresentidx));
      subject := JStringToString(cursor.getString(subjectidx));
      body := JStringToString(cursor.getString(bodyidx));
      servicecenter := JStringToString(cursor.getString(servicecenteridx));
      locked := JStringToString(cursor.getString(lockedidx));
      Listbox1.Items.Add(subject);
      // I plan on deleting messages here
      //SharedActivity.getContentResolver.delete(uri, StringToJString('_ID=' + smsid), nil);
      Result := IntToStr(trunc(msgunixtimestampms/1000)) + ' ' + address + ' ' + body;
    end;
  end;
  </code></pre>
  
  <p>This how I call it:</p>
  
  <pre class="lang-pascal prettyprint-override"><code>procedure TForm1.SpeedButton1Click(Sender: TObject);
  begin
    label1.text := Process_SMS;
  end;
  </code></pre>
  
  <p>Here is the xml file I am using:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;!-- BEGIN_INCLUDE(manifest) --&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.embarcadero.ezt"
      android:versionCode="19"
      android:versionName="1.0.0"
      android:installLocation="preferExternal"&gt;
  
  &lt;!-- This is the platform API where NativeActivity was introduced. --&gt;
  &lt;uses-sdk android:minSdkVersion="9" android:targetSdkVersion="11" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
  &lt;uses-permission android:name="android.permission.BROADCAST_SMS" /&gt;
  &lt;uses-permission android:name="android.permission.BROADCAST_WAP_PUSH" /&gt;
  &lt;uses-permission android:name="android.permission.CALL_PHONE" /&gt;
  &lt;uses-permission android:name="android.permission.CALL_PRIVILEGED" /&gt;
  &lt;uses-permission android:name="android.permission.CAMERA" /&gt;
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="android.permission.READ_CALENDAR" /&gt;
  &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.READ_PHONE_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.READ_SMS" /&gt;
  &lt;uses-permission android:name="android.permission.RECEIVE_MMS" /&gt;
  &lt;uses-permission android:name="android.permission.RECEIVE_SMS" /&gt;
  &lt;uses-permission android:name="android.permission.RECEIVE_WAP_PUSH" /&gt;
  &lt;uses-permission android:name="android.permission.SEND_SMS" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_CALENDAR" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_CALL_LOG" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
  &lt;uses-permission android:name="android.permission.WRITE_SMS" /&gt;
  
  &lt;uses-feature android:glEsVersion="0x00020000" android:required="True"/&gt;
  &lt;application android:persistent="False" 
      android:restoreAnyVersion="False" 
      android:label="ezt" 
      android:debuggable="True" 
      android:largeHeap="False"
      android:icon="@drawable/ic_launcher"
      android:theme="@style/AppTheme"
      android:hardwareAccelerated="true"&gt;
  
  
      &lt;!-- Our activity is a subclass of the built-in NativeActivity framework class.
           This will take care of integrating with our NDK code. --&gt;
      &lt;activity android:name="com.embarcadero.firemonkey.FMXNativeActivity"
              android:label="ezt"
              android:configChanges="orientation|keyboard|keyboardHidden"
              android:launchMode="singleTask"&gt;
          &lt;!-- Tell NativeActivity the name of our .so --&gt;
          &lt;meta-data android:name="android.app.lib_name"
              android:value="ezt" /&gt;
          &lt;intent-filter&gt;  
              &lt;action android:name="android.intent.action.MAIN" /&gt;
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt; 
      &lt;/activity&gt;
  
      &lt;receiver android:name="com.embarcadero.firemonkey.notifications.FMXNotificationAlarm" /&gt;
  
  
  
     &lt;!-- BroadcastReceiver that listens for incoming SMS messages --&gt;
      &lt;receiver android:name=".SmsReceiver"
              android:permission="android.permission.BROADCAST_SMS"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.provider.Telephony.SMS_DELIVER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  
      &lt;!-- BroadcastReceiver that listens for incoming MMS messages --&gt;
      &lt;receiver android:name=".MmsReceiver"
          android:permission="android.permission.BROADCAST_WAP_PUSH"&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" /&gt;
              &lt;data android:mimeType="application/vnd.wap.mms-message" /&gt;
          &lt;/intent-filter&gt;
      &lt;/receiver&gt;
  
      &lt;!-- Activity that allows the user to send new SMS/MMS messages --&gt;
      &lt;activity android:name=".ComposeSmsActivity" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.SEND" /&gt;                
              &lt;action android:name="android.intent.action.SENDTO" /&gt;
              &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;category android:name="android.intent.category.BROWSABLE" /&gt;
              &lt;data android:scheme="sms" /&gt;
              &lt;data android:scheme="smsto" /&gt;
              &lt;data android:scheme="mms" /&gt;
              &lt;data android:scheme="mmsto" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  
      &lt;!-- Service that delivers messages from the phone "quick response" --&gt;
      &lt;service android:name=".HeadlessSmsSendService"
               android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
               android:exported="true" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.RESPOND_VIA_MESSAGE" /&gt;
              &lt;category android:name="android.intent.category.DEFAULT" /&gt;
              &lt;data android:scheme="sms" /&gt;
              &lt;data android:scheme="smsto" /&gt;
              &lt;data android:scheme="mms" /&gt;
              &lt;data android:scheme="mmsto" /&gt;
          &lt;/intent-filter&gt;
      &lt;/service&gt;
  
    &lt;/application&gt;
  &lt;/manifest&gt;
  &lt;!-- END_INCLUDE(manifest) --&gt;
  </code></pre>
  
  <p>On my testing device I have selected my app as the default sms app and under personal->security->app permissions, I have set it to "always allow" my app for READ SMS. The interface for my app is simply a Listbox and two speed buttons.</p>
  
  <p>Please and thanks for your help. I am lost...</p>