關於XE8開發安卓手機硬體
準備工作
一些比較有用的宣告
大小寫轉換:
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<string>;
function Split(const Separator: array of Char; Count: Integer):
TArray<string>;
function Split(const Separator: array of Char; Options:
TStringSplitOptions): TArray<string>;
function Split(const Separator: array of string; Options:
TStringSplitOptions): TArray<string>;
function Split(const Separator: array of Char; Count: Integer; Options:
TStringSplitOptions): TArray<string>;
function Split(const Separator: array of string; Count: Integer;
Options: TStringSplitOptions): TArray<string>;
//---------------------------------------------------------------
連線:
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<string>): string;
class function Join(const Separator: string; const Values:
IEnumerable<string>): string;
class function Join(const Separator: string; const value: array of
string; StartIndex: Integer; Count: Integer): string;
//--------------------------------------------------------------
var
S: string;
str: string;
strArr: TArray<string>;
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<Char>;
function ToCharArray(StartIndex: Integer; Length: Integer):
TArray<Char>;
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;
********************************************************