安卓收发短信 uAndroidSmsManager 2017-01-10
{ ******************************************************************* }
{ }
{ }
{ }
{ 版权所有 (C) 2017 }
{ }
{ ******************************************************************* }
unit uAndroidSmsManager;
{ *******************************************************
模块名称:短信处理单元
功能说明:通过调用Androidapi发送、接收短信
版本:v1.0
程序员姓名: oushengfen
日期: 2017/01/10
更新:
修改者:
修改日期:
******************************************************* }
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes,
System.Variants, FMX.StdCtrls,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
Androidapi.JNI.Net, FMX.Platform, FMX.Helpers.Android,
Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNI.JavaTypes, FMX.MediaLibrary.Actions, FMX.StdActns,
Androidapi.JNIBridge, Androidapi.Helpers,
Androidapi.JNI.Telephony, FMX.Layouts, FMX.Memo,
//FMX.PhoneDialer, //引用该单元,会导致程序运行后,如果手机打电话,会导致程序异常退出
Androidapi.JNI.Os, FMX.ScrollBox, FMX.Controls.Presentation;
{content://sms/inbox 收件箱
content://sms/sent 已发送
content://sms/draft 草稿
content://sms/outbox 发件箱
content://sms/failed 发送失败
content://sms/queued 待发送列表}
const
SMS_URI_ALL = 'content://sms/';{所有的短信}
SMS_URI_INBOX = 'content://sms/inbox';{收件箱短信}
SMS_URI_SEND = 'content://sms/sent'; {已发送短信}
SMS_URI_DRAFT = 'content://sms/draft';{草稿箱短信}
type
TAndroidSmsManager = class
private
TelephonyManager: JTelephonyManager;
public
constructor Create();
destructor Destroy; override;
{获取Android手机SIM卡串号}
procedure getSN;
{获取Android手机IMEI}
function getPhoneIMEI():string;
{向指定手机发送短信}
function sendSMS(phoneNumber, SMSstring: string):Integer;
{接收最新一条短信}
function revSMS(var phoneNumber:string;var body:string; var ididx:Integer):Boolean;
{删除指定序号的短信}
function deleteSMS(ididx:Integer):Integer;
{删除所有短信}
function deleteAllSMS():Boolean;
end;
implementation
{ TAndroidSmsManager }
constructor TAndroidSmsManager.Create;
begin
end;
function TAndroidSmsManager.deleteAllSMS: Boolean;
var
uri: Jnet_Uri;
cursor: JCursor;
ididx:Integer;
begin
Result:=false;
uri := StrToJURI(SMS_URI_ALL); // 收件箱 'content://sms/inbox'
cursor := SharedActivity.getContentResolver.query(uri,
nil, nil, nil, StringToJString('date asc')); // desc 降序
ididx:= cursor.getCount;
if cursor.getCount <> 0 then
begin
while (cursor.moveToNext) do//对所有短信的循环
begin
ididx := cursor.getInt(cursor.getColumnIndex(StringToJString('_id')));
deleteSMS(ididx);
end;
end;
end;
function TAndroidSmsManager.deleteSMS(ididx: Integer): Integer;
begin
Result:= -1;
Result:= SharedActivity.getContentResolver.delete(StrToJURI('content://sms'),
StringToJString('_id='+inttostr(ididx)), nil); // 删除ididx这一条短信
end;
destructor TAndroidSmsManager.Destroy;
begin
inherited;
end;
function TAndroidSmsManager.getPhoneIMEI: string;
begin
GetSN; {先调用一下}
Result:=JStringToString(TelephonyManager.getDeviceId);
end;
procedure TAndroidSmsManager.getSN;
var
TelephonyServiceNative: JObject;
begin
TelephonyServiceNative := SharedActivityContext.getSystemService
(TJContext.JavaClass.TELEPHONY_SERVICE);
if Assigned(TelephonyServiceNative) then
TelephonyManager := TJTelephonyManager.Wrap
((TelephonyServiceNative as ILocalObject).GetObjectID);
end;
function TAndroidSmsManager.revSMS(var phoneNumber, body: string; var ididx:Integer): Boolean;
var
cursor: JCursor;
uri: Jnet_Uri;
address, person, msgdatesent, protocol, msgread, msgstatus, msgtype,
msgreplypathpresent, Subject, servicecenter, locked: string;
msgunixtimestampms: int64;
idhuihuaidx, addressidx, personidx, msgdateidx, msgdatesentidx, protocolidx,
msgreadidx, msgstatusidx, msgtypeidx, msgreplypathpresentidx, subjectidx,
bodyidx, servicecenteridx, lockedidx: integer;
begin
Result:=false;
uri := StrToJURI(SMS_URI_ALL); // 收件箱 'content://sms/inbox'
cursor := SharedActivity.getContentResolver.query(uri,
nil, nil, nil, StringToJString('date asc')); // desc 降序
if cursor.getCount=0 then
begin
//showmessage('符合条件的短信数量为0。');
exit;
end;
// while (cursor.moveToNext) do//对所有短信的循环
// begin
cursor.moveToLast; // 最后一条
ididx := cursor.getInt(cursor.getColumnIndex(StringToJString('_id')));
// 得到当前短信的id号
idhuihuaidx := cursor.getLong(1); // 短信会话的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'));
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));
Result:= true;
{Result := IntToStr(trunc(msgunixtimestampms / 1000)) + #13 + '号码:' + address +
#13 + 'person:' + person + #13 + 'msgdatesent:' + msgdatesent + #13 +
'protocol:' + protocol + #13 + 'msgread:' + msgread + #13 + 'msgstatus:' +
msgstatus + #13 + 'msgtype:' + msgtype + #13 + 'msgreplypathpresent:' +
msgreplypathpresent + #13 + 'Subject:' + Subject + #13 + 'servicecenter:' +
servicecenter + #13 + 'locked:' + locked + #13 + '内容:' + body; }
SharedActivity.getContentResolver.delete
(StrToJURI('content://sms/conversations/' + IntToStr(idhuihuaidx)), nil, nil);
//删除会话idhuihuaidx,即同一个号码的所有短信
{SharedActivity.getContentResolver.delete(StrToJURI('content://sms'),
StringToJString('_id='+inttostr(ididx)), nil); // 删除ididx这一条短信}
end;
function TAndroidSmsManager.sendSMS(phoneNumber, SMSstring: string): Integer;
var
j: JSmsManager;
begin
Result:=0;
try
j := TJSmsManager.JavaClass.getDefault();
j.sendMultipartTextMessage(StringToJString(phoneNumber), nil,
j.divideMessage(StringToJString(SMSstring)), nil, nil); //sendTextMessage
except
Result:= -1;
end;
end;
end.