手机震动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;
var
  LIndex: Integer;
begin
  Result := TJavaArray.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;
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;
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;
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.

 

你可能感兴趣的:(XE5,RootKit)