The source code is as follows:
[Delphi]View PlainCopy
- 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) does
- Result. Items[lindex]: = Aintarr[lindex];
- End
- Procedure Vibratortest;
- Var
- Lvibratorobj:jobject;
- Lvibrator:jvibrator;
- ljavaarray:tjavaarray<int64>;
- Begin
- {Vibrator Summary:
- Cancel (): Off vibration
- Hasvibrator (): Check if hardware is supported
- Vibrate (long milliseconds): vibration milliseconds ms
- Vibrate (long[] pattern, int repeat): Vibrate by given array}
- {You need to turn on vibrator permissions}
- //<del>lvibrator: = tjvibrator.create as jvibrator;</del>
- {Create 2014-5-8 update using the official recommended method}
- Lvibratorobj: = Sharedactivity. Getsystemservice (
- Tjcontext. Javaclass. Vibrator_service);
- Lvibrator: = Tjvibrator. Wrap ((lvibratorobj as ilocalobject). Getobjectid);
- {Test whether the phone supports vibration}
- if not lvibrator. Hasvibrator Then
- begin
- ShowMessage (' mobile phone does not support vibration ');
- Exit;
- end;
- {Test procedure vibrate (milliseconds:int64); cdecl; overload;}
- {Effect A: immediate vibration 800 milliseconds}
- Lvibrator.vibrate (800);
- {Test procedure vibrate (pattern:tjavaarray<int64>; repeat_: Integer); cdecl; overload;
- Pattern: For vibrating array parameters, even for waiting time (ms), odd for vibration duration (ms)
- Repeat_:-1: Shake only once; >-1: Start repeating vibrations from the position of index Repeat_}
- {Create Test array}
- Ljavaarray: = Getvibratorarray ([3000, +, + ]);
- {effect B: Wait 500 milliseconds, vibration 1 seconds, wait 2 seconds, shake 3 seconds}
- Lvibrator.vibrate (Ljavaarray,-1);
- {effect c: effect B repeats vibration}
- Lvibrator.vibrate (Ljavaarray, 0);
- {Cancel vibrate (automatically cancels when the phone is in a dark screen or lock screen)}
- Lvibrator.cancel;
- {effect D: (wait 500 milliseconds, vibration 1 seconds, wait for 2 seconds, shake 3 seconds) (shake first in the original order)
- then loop [1000, 2000, 3000]
- (Wait 1 seconds, shake 2 seconds-> Wait 3 seconds)
- ->[wait 1 seconds--wait 2 seconds ...]
- This sounds like (wait 4 seconds, shake 2 seconds)}
- //Lvibrator.vibrate (Ljavaarray, 1);
- {effect E: (Executed once in the original order), then no vibration (even waiting time)}
- Lvibrator.vibrate (Ljavaarray, 3);
- {effect F: Of course, Report indexoutbounds exception}
- Lvibrator.vibrate (Ljavaarray, {!!!} 4);
- End
http://blog.csdn.net/flcop/article/details/13290779
Delphi XE5 Android Phone vibrate (through Jobject test support vibration)