其他分享
首页 > 其他分享> > android – 如何从收件箱中删除短信?

android – 如何从收件箱中删除短信?

作者:互联网

我编写了一个自动短信响应器:

.
.
.
function IsSMSAnswered(smsid: string): Boolean;
begin
  // Code to test if SMS is answered
end;

procedure SendReplySMS(reply_smssender, reply_smsbody: string);
begin
  // Code to send reply to smssender
end;

procedure FetchSMS;  //fetch sms used by timer every 1 minute
var
  cursor: JCursor;
  uri: Jnet_Uri;

  id_smsid: integer;
  id_smssender: integer;
  id_smsbody: integer;

  id_smsid: string;
  smssender: string;
  smsbody: string;
  smsreply: string;
begin
  uri:= StrToJURI('content://sms/inbox');
  cursor:= SharedActivity.getContentResolver.query(uri, nil, nil, nil, nil);

  id_smsid:= cursor.getColumnIndex(StringToJstring('_id'));
  id_smssender:= cursor.getColumnIndex(StringToJstring('address'));
  id_smsbody:=cursor.getColumnIndex(StringToJstring('body'));

  cursor.moveToLast;
  for i:= cursor.getCount downto 1 do
    begin
      smsid:= :=JStringToString(cursor.getString(id_smsid));

      if not IsSMSAnswered(smsid) then
        begin
          smssender:=JStringToString(cursor.getString(id_smssender));
          smsbody:=JStringToString(cursor.getString(id_smssender));

          if smsbody = 'abc' then
            SendReplySMS(smssender, 'reply abc')
          else if smsbody = 'def' then
            SendReplySMS(smssender, 'reply def');
        end
      else
        Break;
    end;
end;
.
.
.

在这种情况下,仍然在电话数据库中收到所有收到的短信.

处理后如何从手机数据库中删除收到的短信?

.
.
.
if not IsSMSAnswered(smsid) then
  begin
    smssender:=JStringToString(cursor.getString(id_smssender));
    smsbody:=JStringToString(cursor.getString(id_smssender));

    if smsbody = 'abc' then
      SendReplySMS(smssender, 'reply abc')
    else if smsbody = 'def' then
      SendReplySMS(smssender, 'reply def');

    ???? Now do I delete this received message ????
  end
.
.
.

解决方法:

我有一个解决方案(在pskink的帮助下):

.
.
.
procedure FetchSMS;  //fetch sms used by timer every 1 minute
var
  cursor: JCursor;
  uri: Jnet_Uri;
  uri_del: Jnet_Uri;

  id_smsid: integer;
  id_smssender: integer;
  id_smsbody: integer;

  smsid: string;
  smssender: string;
  smsbody: string;
  smsreply: string;
begin
  uri:= StrToJURI('content://sms/inbox');
 cursor:= SharedActivity.getContentResolver.query(uri, nil, nil, nil, nil);

  id_smsid:= cursor.getColumnIndex(StringToJstring('_id'));
  id_smssender:= cursor.getColumnIndex(StringToJstring('address'));
  id_smsbody:=cursor.getColumnIndex(StringToJstring('body'));

  cursor.moveToLast;
  for i:= cursor.getCount downto 1 do
    begin
      smsid:= :=JStringToString(cursor.getString(id_smsid));

      if not IsSMSAnswered(smsid) then
        begin
          smssender:=JStringToString(cursor.getString(id_smssender));
          smsbody:=JStringToString(cursor.getString(id_smssender));

          if smsbody = 'abc' then
            SendReplySMS(smssender, 'reply abc')
          else if smsbody = 'def' then
            SendReplySMS(smssender, 'reply def');

          // ???? Now I need to delete this received message ????

          // There is the answer
          uri_del:= StrToJURI('content://sms');
          SharedActivity.getContentResolver.delete(uri_del, StringToJString('_ID=' + smsid), nil);
          // There is the answer
        end
      else
        Break;
    end;
end;

.
.
.

标签:android,sms,delphi,firemonkey,delphi-xe5
来源: https://codeday.me/bug/20190624/1279434.html