Delphi XE5 Android 调用手机震动(通过JObject测试是否支持震动)


本文整理自网络,侵删。

 
uses  
  Androidapi.JNI.Os,  
  Androidapi.JNIBridge;  
  
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 VibratorTest;  
var  
  LVibratorObj: JObject;  
  LVibrator: JVibrator;  
  LJavaArray: TJavaArray<Int64>;  
begin  
  { Vibrator概要: 
      cancel(): 关闭震动 
      hasVibrator():检查硬件是否支持 
      vibrate(long milliseconds): 震动milliseconds毫秒 
      vibrate(long[] pattern, int repeat):按给定数组震动 }  
  
  { 需要开启Vibrator权限 }  
  // <del>LVibrator := TJVibrator.Create as JVibrator;</del>  
  { 使用官方推荐方式创建 2014-5-8 update}  
  LVibratorObj := SharedActivity.getSystemService(  
    TJContext.JavaClass.VIBRATOR_SERVICE);    
  LVibrator := TJVibrator.Wrap((LVibratorObj as ILocalObject).GetObjectID);  
  
  { 测试手机是否支持震动 }  
  if not LVibrator.hasVibrator then  
  begin  
    ShowMessage('手机不支持震动');  
    Exit;  
  end;  
  
{ Test procedure vibrate(milliseconds: Int64); cdecl; overload; }  
  
  { 效果A: 立即震动 800 毫秒 }  
//  LVibrator.vibrate(800);  
  
{ Test procedure vibrate(pattern: TJavaArray<Int64>; repeat_: Integer); cdecl; overload; 
   pattern: 为震动数组参数,偶数代表等待时间(ms), 奇数代表震动持续时间(ms) 
   repeat_: -1: 只震动一遍;  > -1: 从Index为 repeat_ 的位置开始重复震动 }  
  
  { 创建测试数组 }  
  LJavaArray := GetVibratorArray([500, 1000, 2000, 3000]);  
  
  { 效果B: 等待500毫秒 -> 震动1秒 -> 等待2秒 -> 震动3秒 }  
//  LVibrator.vibrate(LJavaArray, -1);  
  
  { 效果C: 效果B重复震动 }  
//  LVibrator.vibrate(LJavaArray, 0);  
  
  { 取消震动(当手机暗屏或锁屏时会自动取消) }  
//  LVibrator.cancel;  
  
  { 效果D: (等待500毫秒 -> 震动1秒 -> 等待2秒 -> 震动3秒)(先按原顺序震动一遍) 
           接着循环 [1000, 2000, 3000] 
           ->(等待1秒 -> 震动2秒 - > 等待3秒) 
           ->[等待1秒 -> 等待2秒 ... ] 
           这个听上去的效果像( 等待4秒 -> 震动2秒 )}  
  // LVibrator.vibrate(LJavaArray, 1);  
  
  { 效果E: (先按原顺序执行一遍), 接着不会震动(偶数为等待时间) }  
//  LVibrator.vibrate(LJavaArray, 3);  
  
  { 效果F: 当然是报IndexOutBounds异常 }  
//  LVibrator.vibrate(LJavaArray, {!!!}4);  
  
end;  

相关阅读 >>

Delphi 网上获取北京时间验证码识别之中值滤波

Delphi 把exe嵌入到自己的exe中。Delphi xe3

Delphi access数据库密码的mdb的访问报错“无法启动应用程序,或是已被其他用户已独占方式打开”

Delphi 取系统临时路径

Delphi2010新功能:tdirectory.getfiles 支持通配符

Delphi整理二(object pascal语言)

Delphi 将tbitmap与tgpimage转换

Delphi xe10 传感器操作

Delphi查找特定的exe是否在运行

解决Delphi程序在非中文系统下乱码

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...