2014-03-29 73 views
4

我編程的短信自動應答:如何從收件箱中刪除短信?

. 
. 
. 
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 
. 
. 
. 
+0

此解決方案不適用於Delphi ... – pudnivec74

+0

您可以用非常相似的方式將它們刪除 – pskink

+0

您能告訴我怎麼做嗎? – pudnivec74

回答

4

我有一個解決方案(由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; 

。 。 。

+0

提供解決方案的榮譽,+1 – Johan

+0

我的大腦編譯器告訴我,由於id_smsid變量加倍,將無法編譯。 – TLama

+0

Cursor.getColumnIndex總是返回整數值。 – pudnivec74

相關問題