How to read SMS with C++ Builder in Android

522 Views Asked by At

This time I try to read sms using C++ Builder environment and what I found in internet is one example written in Delphi. I do not need all information from this example so I crack this code to one parametr. So the example looks like this and is taken from this post. How to code read sms messages from android kitkat Delphi XE8

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;

In my example I want to read only body of the sms message and looks like this:

_di_JCursor cursor;
  _di_Jnet_Uri uri;

   uri = StrToJURI( "content://sms/inbox" );
   cursor = SharedActivity()->getContentResolver()->query( uri, nullptr, nullptr, nullptr, nullptr );

   int bodyidx = cursor->getColumnIndex( StringToJString("body") );
   String body;

   while ( cursor->moveToNext )
   {
    body = JStringToString( cursor->getString(bodyidx) );
   }

   Memo1->Lines->Add(body);

I have some old and one new sms and after click button to read sms I got errors which I do not understand:

First chance exception at $61C3F582. Exception class Bus error (10). Process Project1.apk (4600) First chance exception at $61D2B76E. Exception class Segmentation fault (11). Process Project1.apk (4731)

I ask about suggestion about what is wrong with code or something else

0

There are 0 best solutions below