码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2017-06-19 20:54:14      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:get   pat   取消   mil   boa   toc   net   com   []   

源码如下:

[delphi] view plain copy
 
  1. uses  
  2.   Androidapi.JNI.Os,  
  3.   Androidapi.JNIBridge;  
  4.   
  5. function GetVibratorArray(const AIntArr: array of Int64): TJavaArray<Int64>;  
  6. var  
  7.   LIndex: Integer;  
  8. begin  
  9.   Result := TJavaArray<Int64>.Create(Length(AIntArr));  
  10.   for LIndex := Low(AIntArr) to High(AIntArr) do  
  11.     Result.Items[LIndex] := AIntArr[LIndex];  
  12. end;  
  13.   
  14. procedure VibratorTest;  
  15. var  
  16.   LVibratorObj: JObject;  
  17.   LVibrator: JVibrator;  
  18.   LJavaArray: TJavaArray<Int64>;  
  19. begin  
  20.   { Vibrator概要: 
  21.       cancel(): 关闭震动 
  22.       hasVibrator():检查硬件是否支持 
  23.       vibrate(long milliseconds): 震动milliseconds毫秒 
  24.       vibrate(long[] pattern, int repeat):按给定数组震动 }  
  25.   
  26.   { 需要开启Vibrator权限 }  
  27.   // <del>LVibrator := TJVibrator.Create as JVibrator;</del>  
  28.   { 使用官方推荐方式创建 2014-5-8 update}  
  29.   LVibratorObj := SharedActivity.getSystemService(  
  30.     TJContext.JavaClass.VIBRATOR_SERVICE);    
  31.   LVibrator := TJVibrator.Wrap((LVibratorObj as ILocalObject).GetObjectID);  
  32.   
  33.   { 测试手机是否支持震动 }  
  34.   if not LVibrator.hasVibrator then  
  35.   begin  
  36.     ShowMessage(‘手机不支持震动‘);  
  37.     Exit;  
  38.   end;  
  39.   
  40. { Test procedure vibrate(milliseconds: Int64); cdecl; overload; }  
  41.   
  42.   { 效果A: 立即震动 800 毫秒 }  
  43. //  LVibrator.vibrate(800);  
  44.   
  45. { Test procedure vibrate(pattern: TJavaArray<Int64>; repeat_: Integer); cdecl; overload; 
  46.    pattern: 为震动数组参数,偶数代表等待时间(ms), 奇数代表震动持续时间(ms) 
  47.    repeat_: -1: 只震动一遍;  > -1: 从Index为 repeat_ 的位置开始重复震动 }  
  48.   
  49.   { 创建测试数组 }  
  50.   LJavaArray := GetVibratorArray([500, 1000, 2000, 3000]);  
  51.   
  52.   { 效果B: 等待500毫秒 -> 震动1秒 -> 等待2秒 -> 震动3秒 }  
  53. //  LVibrator.vibrate(LJavaArray, -1);  
  54.   
  55.   { 效果C: 效果B重复震动 }  
  56. //  LVibrator.vibrate(LJavaArray, 0);  
  57.   
  58.   { 取消震动(当手机暗屏或锁屏时会自动取消) }  
  59. //  LVibrator.cancel;  
  60.   
  61.   { 效果D: (等待500毫秒 -> 震动1秒 -> 等待2秒 -> 震动3秒)(先按原顺序震动一遍) 
  62.            接着循环 [1000, 2000, 3000] 
  63.            ->(等待1秒 -> 震动2秒 - > 等待3秒) 
  64.            ->[等待1秒 -> 等待2秒 ... ] 
  65.            这个听上去的效果像( 等待4秒 -> 震动2秒 )}  
  66.   // LVibrator.vibrate(LJavaArray, 1);  
  67.   
  68.   { 效果E: (先按原顺序执行一遍), 接着不会震动(偶数为等待时间) }  
  69. //  LVibrator.vibrate(LJavaArray, 3);  
  70.   
  71.   { 效果F: 当然是报IndexOutBounds异常 }  
  72. //  LVibrator.vibrate(LJavaArray, {!!!}4);  
  73.   
  74. end;  


 

http://blog.csdn.net/flcop/article/details/13290779

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

标签:get   pat   取消   mil   boa   toc   net   com   []   

原文地址:http://www.cnblogs.com/findumars/p/7050427.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!