准备工作
一些比较有用的声明
大小写转换:
function ToLower: string;
function ToLower(LocaleID: TLocaleID): string;
function ToLowerInvariant: string;
function ToUpper: string;
function ToUpper(LocaleID: TLocaleID): string;
function ToUpperInvariant: string;
class function LowerCase(const S: string): string;
class function LowerCase(const S: string; LocaleOptions:
TLocaleOptions): string;
class function UpperCase(const S: string): string;
class function UpperCase(const S: string; LocaleOptions:
TLocaleOptions): string;
//---------------------------------------------------------------
var
str: string;
begin
str := 'Delphi';
str := str.ToLower; // delphi
str := str.ToUpper; // DELPHI
end;
清除两边空格或指定字符:
function Trim: string;
function TrimLeft: string;
function TrimRight: string;
function Trim(const TrimChars: array of Char): string;
function TrimLeft(const TrimChars: array of Char): string;
function TrimRight(const TrimChars: array of Char): string;
字符串对比
function CompareTo(const strB: string): Integer;
class function Compare(const StrA: string; const StrB: string):
Integer;
class function CompareText(const StrA: string; const StrB: string):
Integer;
class function Compare(const StrA: string; const StrB: string;
LocaleID: TLocaleID): Integer;
class function Compare(const StrA: string; const StrB: string;
IgnoreCase: Boolean): Integer;
class function Compare(const StrA: string; const StrB: string;
IgnoreCase: Boolean; LocaleID: TLocaleID): Integer;
class function Compare(const StrA: string; IndexA: Integer; const StrB:
string; IndexB: Integer; Length: Integer): Integer;
class function Compare(const StrA: string; IndexA: Integer; const StrB:
string; IndexB: Integer; Length: Integer; LocaleID: TLocaleID):
Integer;
class function Compare(const StrA: string; IndexA: Integer; const StrB:
string; IndexB: Integer; Length: Integer; IgnoreCase: Boolean):
Integer;
class function Compare(const StrA: string; IndexA: Integer; const StrB:
string; IndexB: Integer; Length: Integer; IgnoreCase: Boolean;
LocaleID: TLocaleID): Integer;
class function CompareOrdinal(const StrA: string; const StrB: string):
Integer;
class function CompareOrdinal(const StrA: string; IndexA: Integer;
const StrB: string; IndexB: Integer; Length: Integer): Integer;
//---------------------------------------------------------------
搜索字符串
搜索字符串:
function IndexOf(value: Char): Integer;
function IndexOf(const Value: string): Integer;
function IndexOf(Value: Char; StartIndex: Integer): Integer;
function IndexOf(const Value: string; StartIndex: Integer): Integer;
function IndexOf(Value: Char; StartIndex: Integer; Count: Integer):
function IndexOf(const Value: string; StartIndex: Integer; Count:
Integer): Integer;
function IndexOfAny(const AnyOf: array of Char): Integer;
function IndexOfAny(const AnyOf: array of Char; StartIndex: Integer):
Integer;
function IndexOfAny(const AnyOf: array of Char; StartIndex: Integer;
Count: Integer): Integer;
function LastIndexOf(Value: Char): Integer;
function LastIndexOf(const Value: string): Integer;
function LastIndexOf(Value: Char; StartIndex: Integer): Integer;
function LastIndexOf(const Value: string; StartIndex: Integer):
Integer;
function LastIndexOf(Value: Char; StartIndex: Integer; Count:
Integer): Integer;
function LastIndexOf(const Value: string; StartIndex: Integer; Count:
Integer): Integer;
function LastIndexOfAny(const AnyOf: array of Char): Integer;
function LastIndexOfAny(const AnyOf: array of Char; StartIndex:
Integer): Integer;
function LastIndexOfAny(const AnyOf: array of Char; StartIndex:
Integer; Count: Integer): Integer;
//---------------------------------------------------------------
是否包含:
function Contains(const Value: string): Boolean;
function StartsWith(const Value: string): Boolean;
function StartsWith(const Value: string; IgnoreCase: Boolean):
Boolean;
function EndsWith(const Value: string): Boolean;
function EndsWith(const Value: string; IgnoreCase: Boolean): Boolean;
class function EndsText(const ASubText, AText: string): Boolean;
//---------------------------------------------------------------
添加或解除引号:
function QuotedString: string;
function QuotedString(const QuoteChar: Char): string;
function DeQuotedString: string;
function DeQuotedString(const QuoteChar: Char): string;
//---------------------------------------------------------------
适宽处理:
function PadLeft(TotalWidth: Integer): string;
function PadLeft(TotalWidth: Integer; PaddingChar: Char): string;
function PadRight(TotalWidth: Integer): string;
function PadRight(TotalWidth: Integer; PaddingChar: Char): string;
//---------------------------------------------------------------
var
str: string;
begin
str := '1';
str := str.PadLeft(4, '0'); // 0001
end;
插入与删除:
function Insert(StartIndex: Integer; const Value: string): string;
function Remove(StartIndex: Integer): string;
function Remove(StartIndex: Integer; Count: Integer): string;
//---------------------------------------------------------------
截取:
function Substring(StartIndex: Integer): string;
function Substring(StartIndex: Integer; Length: Integer): string;
//---------------------------------------------------------------
var
str1, str2: string;
begin
str1 := 'Delphi XE4';
str2 := str1.Substring(7); // XE4
str2 := str1.Substring(7, 2); // XE
end;
替换:
function Replace(OldChar: Char; NewChar: Char): string;
function Replace(OldChar: Char; NewChar: Char; ReplaceFlags:
TReplaceFlags): string;
function Replace(const OldValue: string; const NewValue: string):
string;
function Replace(const OldValue: string; const NewValue: string;
ReplaceFlags: TReplaceFlags): string;
//--------------------------------------------------------------
var
str1, str2: string;
begin
str1 := 'ABC ABC ABC';
str2 := str1.Replace('A', '*'); // *BC *BC *BC
str2 := str1.Replace('A', '*', [rfIgnoreCase]); // *BC ABC ABC
end;
分割:
function Split(const Separator: array of Char): TArray
function Split(const Separator: array of Char; Count: Integer):
TArray
function Split(const Separator: array of Char; Options:
TStringSplitOptions): TArray
function Split(const Separator: array of string; Options:
TStringSplitOptions): TArray
function Split(const Separator: array of Char; Count: Integer; Options:
TStringSplitOptions): TArray
function Split(const Separator: array of string; Count: Integer;
Options: TStringSplitOptions): TArray
//---------------------------------------------------------------
连接:
class function Join(const Separator: string; const values: array of
const): string;
class function Join(const Separator: string; const Values: array of
string): string;
class function Join(const Separator: string; const Values:
IEnumerator
class function Join(const Separator: string; const Values:
IEnumerable
class function Join(const Separator: string; const value: array of
string; StartIndex: Integer; Count: Integer): string;
//--------------------------------------------------------------
var
S: string;
str: string;
strArr: TArray
begin
str := 'A1,B2,C3,,,,D4,E5,F6,G7';
strArr := str.Split([','], ExcludeEmpty);
str := S.Join('-', strArr); // A1-B2-C3-D4-E5-F6-G7
str := S.Join('; ', [1,2,3,4,5]); // 1; 2; 3; 4; 5
str := S.Join(',', ['abc', 123, true]); // abc,123,True
end;
类型转换:
function ToBoolean: Boolean;
function ToInteger: Integer;
function ToSingle: Single;
function ToDouble: Double;
function ToExtended: Extended;
class function ToBoolean(const S: string): Boolean;
class function ToInteger(const S: string): Integer;
class function ToSingle(const S: string): Single;
class function ToDouble(const S: string): Double;
class function ToExtended(const S: string): Extended;
定界符:
function IsDelimiter(const Delimiters: string; Index: Integer):
Boolean;
function LastDelimiter(const Delims: string): Integer;
//---------------------------------------------------------------
空字符串:
const Empty = '';
function IsEmpty: Boolean;
class function IsNullOrEmpty(const Value: string): Boolean;
class function IsNullOrWhiteSpace(const Value: string): Boolean;
//--------------------------------------------------------------
String 与 Char:
class function Create(C: Char; Count: Integer): string;
class function Create(const Value: array of Char; StartIndex: Integer;
Length: Integer): string;
class function Create(const Value: array of Char): string;
property Chars[Index: Integer]: Char read GetChars;
property Length: Integer read GetLength;
function CountChar(const C: Char): Integer;
function ToCharArray: TArray
function ToCharArray(StartIndex: Integer; Length: Integer):
TArray
procedure CopyTo(SourceIndex: Integer; var destination: array of Char;
DestinationIndex: Integer; Count: Integer);
//---------------------------------------------------------------
接下来重要的打电话
9 打电话、发短信和邮件,取得手机 IMEI 号
以下实现对电话功能的基本操作。
1、引用以下单元
uses
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.PhoneDialer , Androidapi.JNI.Os
2、建立如下函数
public
{ Public declarations }
TelephonyManager: JTelephonyManager;
procedure Call_URI(const AAction: JString; const AURI: string);
// 打电话、打开地图显示某个坐标点 、发送电子邮件、播放音乐
procedure PhoneCall(phoneNumber: string); // 打电话
procedure GetSN; // 获取Android手机 SIM卡串号
procedure SentSMS(phoneNumber , SMSstring: string); // 直接没有任何提示的发送短信
procedure SentSMSfromIntent(phoneNumber , SMSstring: string); // 调用系统程序发短信
function FetchSms: string; // 收短信
3、实现函数功能
procedure TForm1.PhoneCall(phoneNumber: string); // 打电话
var
phone: IFMXPhoneDialerService;
begin
if TPlatformServices.Current.SupportsPlatformService(IFMXPhoneDialerService,
IInterface(phone)) then
begin
phone.Call(phoneNumber);
// 监听电话状态请用phone.OnCallStateChanged事件
end;
end;
procedure TForm1.SentSMSfromIntent(phoneNumber , SMSstring: string);
// 调用系统程序发短信
var
uri: Jnet_Uri;
Intent: JIntent;
begin
uri := StrToJURI('smsto:' + phoneNumber);
Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_SENDTO, uri);
Intent.putExtra(StringToJString('sms_body'), StringToJString(SMSstring));
SharedActivityContext.startActivity(Intent);
end;
procedure TForm1.SentSMS(phoneNumber , SMSstring: string);
// 直接没有任何提示的发送短信
var
j: JSmsManager;
begin
j := tjsmsmanager .Create;
j.sendMultipartTextMessage(StringToJString(phoneNumber), nil,
j.divideMessage(StringToJString(SMSstring)), nil, nil);
end;
procedure TForm1.GetSN; // 获取Android手机SIM卡串号
var
TelephonyServiceNative: JObject;
begin
TelephonyServiceNative := SharedActivityContext.getSystemService
(TJContext.JavaClass.TELEPHONY_SERVICE);
if Assigned(TelephonyServiceNative) then
TelephonyManager := TJTelephonyManager .Wrap
((TelephonyServiceNative as ILocalObject).GetObjectID);
// TelephonyManager .getDeviceId 取IMEI
// TelephonyManager .getLine1Number 取 MSISDN 手机号,大部分 SIM 卡中不会写入这个
信息
// TelephonyManager .getSimSerialNumber 取 ICCID
// TelephonyManager .getSubscriberId 取 IMSI 运营商实际上是用这个查询的他那张对应
电话号码的表
end;
function TForm1.FetchSms: string; // 收短信
var
cursor: JCursor;
uri: Jnet_Uri;
address, person, msgdatesent, protocol, msgread, msgstatus, msgtype,
msgreplypathpresent, Subject, body, servicecenter , locked: string;
msgunixtimestampms: int64;
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);
cursor := SharedActivity.
managedQuery(
StrToJURI('content://sms/inbox'), //StrToJURI('content://sms/')所有短信, 含
发件箱
nil,
StringToJString('1=1) group by (address'),//类似于 SQL语句,注意,括号只
有一半,原因中它已经有一对括号了
nil,
StringToJString('date asc')); //desc 降序
//以上执行的语句是:SELECT * FROM sms WHERE (type=1) AND (1=1) group by (address)
order by date asc
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
cursor .moveToLast;//最后一条
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 := 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;
// end;
end;
********************************************************