You can use the mobile phone's vibration function to send a specific style of vibration to the mobile phone for notification. Android allows you to control the vibration style. You can use the vibration to convey information to get users' attention.
To set the vibration style, set a time array for the vibrate attribute of notification. Construct an array. The numbers at each interval correspond to the duration of the vibration or pause (in millimeters ).
Before using vibration in your program, you need to grant permissions to the program. Use the following code snippet to add a uses-Permission node to the application:
<Uses-Permission Android: Name = "android. Permission. Vibrate"/>
The following example shows how to modify the vibration of notification. In the repeated mode, the system vibrates for one second and stops for one second, for a total of five seconds.
Long [] vibrate = new long [] {1000,100 0, 1000,100 0, 1000 };
Notification. vibrate = vibrate;
You can use this subtle control to transmit information to users. Next, we will update the announcenewquake method and set the vibration style of the mobile phone based on the earthquake level. Earthquakes are measured exponentially. Therefore, you can use the same measurement method to create a vibration style.
For a very small magnitude 1 earthquake that can be felt, the mobile phone will shake a little bit of time; but for a magnitude 10 earthquake, it can tear the earth into two halves, and then the mobile phone will shake for 20 seconds. For most earthquakes between 3 and 7, mobile phones will shake between 200mm and 4 seconds.
Double vibratelength = 100 * Math. exp (0.53 * quake. getmagnitude ());
Long [] vibrate = new long [] {100,100, (long) vibratelength };
Newearquakenotification. vibrate = vibrate;
The current Android simulator cannot intuitively or hear the vibration of the device. To verify that notification works properly, you can monitor the log output of "vibration on"/"vibration off.