Delphi xe7 up1 調用android振動功能
振動用到以下4個單元:
Androidapi.JNI.App,Androidapi.JNIBridge,Androidapi.JNI.Os,Androidapi.Helpers
使用方法:
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毫秒
最後在Project Options中設置Uses Permissions將Vibrate設為true
詳細程序如下:
unit Unit6;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,Androidapi.JNI.App,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
Androidapi.JNIBridge,Androidapi.JNI.Os,Androidapi.Helpers;
type
TForm6 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form6: TForm6;
implementation
{$R *.fmx}
procedure TForm6.Button1Click(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;
end.