Android实例-手机震动(XE8+小米2)

时间:2021-12-16 22:56:32

Android实例-手机震动(XE8+小米2)

相关资料:http://blog.csdn.net/laorenshen/article/details/41148843

结果:

1.打开Vibrate权限为True。

2.规律震动我没感觉出来,有了解情况的请联系我。多谢了。

实例代码:

 unit Unit1;

 interface

 uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
Androidapi.Helpers,//
FMX.Helpers.Android,//
Androidapi.JNI.App,//
Androidapi.JNI.Os,//
Androidapi.JNIBridge,//
FMX.StdCtrls, FMX.Controls.Presentation;//
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.fmx}
{$R *.NmXhdpiPh.fmx ANDROID} 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);
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();//震动200ms
end; procedure TForm1.Button2Click(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.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([, , , ]);//调用震动规律//我没感觉出来,如果你们能感觉出来,给我说一下。
LVibrator.vibrate(LJavaArray, -);//震动一遍
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([, , , ]);//调用震动规律
LVibrator.vibrate(LJavaArray, );//重复 //-1:震动一遍 0:重复 >0:指定震动遍数
end; end.