这次我尝试使用C ++ Builder环境读取短信,我在互联网上找到的是用Delphi编写的一个例子。我不需要此示例中的所有信息,因此我将此代码破解为一个参数。所以这个例子看起来像这样,并从这篇文章中获取。 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;
在我的例子中,我想只读取短信的主体,看起来像这样:
_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);
我有一些旧的和一个新的短信,点击按钮后读取短信我得到了我不明白的错误:
$ 61C3F582的第一次机会异常。异常类总线错误(10)。 处理Project1.apk(4600)$ 61D2B76E的第一次机会异常。 异常类分段错误(11)。处理Project1.apk(4731)
我询问关于代码或其他什么问题的建议