手機震動android程式碼
unit MobelZD;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls;
type
TForm1 = class(TForm)
StyleBook1: TStyleBook;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Label1: TLabel;
Button5: TButton;
Button6: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
uses
FMX.Helpers.Android,
Androidapi.JNI.App,
Androidapi.JNI.Os,
Androidapi.JNIBridge,
mobel2;
//各按鈕對應程式碼:
//生成振動規律陣列,如AIntArr 為[500, 1000, 2000, 3000]表示等半秒 -> 震1秒 -> 等2秒 -> 震3秒,數字單位為毫秒。
function GetVibratorArray(const AIntArr: array of Int64): TJavaArray<Int64>;
var
LIndex: Integer;
begin
Result := TJavaArray<Int64>.Create(Length(AIntArr));
for LIndex := Low(AIntArr) to High(AIntArr) do
Result.Items[LIndex] := AIntArr[LIndex];
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Text := '52 如何呼叫手機震動!';
end;
procedure TForm1.Button2Click(Sender: TObject);
var
LVibrator: JVibrator;
begin
LVibrator := TJVibrator.Wrap
((SharedActivity.getSystemService(TJActivity.JavaClass.VIBRATOR_SERVICE)
as ILocalObject).GetObjectID);//呼叫振動
if not LVibrator.hasVibrator then
begin
ShowMessage('手機不支援震動');
Exit;
end;
LVibrator.vibrate(500);//振動500毫秒
end;
procedure TForm1.Button3Click(Sender: TObject);
var
LVibrator: JVibrator;
LJavaArray: TJavaArray<Int64>;
begin
LVibrator := TJVibrator.Wrap
((SharedActivity.getSystemService(TJActivity.JavaClass.VIBRATOR_SERVICE)
as ILocalObject).GetObjectID); //呼叫振動
if not LVibrator.hasVibrator then
begin
ShowMessage('手機不支援震動');
Exit;
end;
LJavaArray := GetVibratorArray([500, 1000, 2000, 3000]);//指出振動規律
LVibrator.vibrate(LJavaArray, -1);//不重複,只按振動規律振動一個迴圈
end;
procedure TForm1.Button4Click(Sender: TObject);
var
LVibrator: JVibrator;
LJavaArray: TJavaArray<Int64>;
begin
LVibrator := TJVibrator.Wrap
((SharedActivity.getSystemService(TJActivity.JavaClass.VIBRATOR_SERVICE)
as ILocalObject).GetObjectID); //呼叫振動
if not LVibrator.hasVibrator then
begin
ShowMessage('手機不支援震動');
Exit;
end;
LJavaArray := GetVibratorArray([500, 1000, 2000, 3000]); //指出振動規律
LVibrator.vibrate(LJavaArray, 0); //按振動規律振動,不停重複。其它大於0的數字可以指定振動次數
end;
procedure TForm1.Button5Click(Sender: TObject);
var
LVibrator: JVibrator;
LJavaArray: TJavaArray<Int64>;
begin
LVibrator := TJVibrator.Wrap
((SharedActivity.getSystemService(TJActivity.JavaClass.VIBRATOR_SERVICE)
as ILocalObject).GetObjectID); //呼叫振動
LVibrator.cancel; //立即停止振動
end;
procedure TForm1.Button6Click(Sender: TObject);
begin
mobel2.Form2.Show;
end;
end.