Step 1: Layout file:
The main. xml Code is as follows: View Code
1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 3 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 4 5 android: layout_width = "fill_parent" 6 7 android: layout_height = "fill_parent" 8 9 android: orientation = "vertical"> 10 11 12 13 14 <Chronometer15 16 android: id = "@ + id/myChronometer" 17 18 android: layout_width = "fill_parent" 19 20 android: layout_height = "wrap_content"/> 21 22 23 24 25 <LinearLayout26 27 android: layout_width = "fill_parent" 28 29 android: layout_height = "wrap_content" 30 31 android: orientation = "horizontal"> 32 33 34 35 36 <Button37 38 android: id = "@ + id/btn_start" 39 40 android: layout_width = "wrap_content" 41 42 android: layout_height = "wrap_content" 43 44 android: text = ""/> 45 46 47 48 49 <Button50 51 android: id = "@ + id/btn_stop" 52 53 android: layout_width = "wrap_content" 54 55 android: layout_height = "wrap_content" 56 57 android: text = "stop"/> 58 59 60 61 62 <Button63 64 android: id = "@ + id/btn_base" 65 66 android: layout_width = "wrap_content" 67 68 android: layout_height = "wrap_content" 69 70 android: text = "reset"/> 71 72 73 74 75 <Button76 77 android: id = "@ + id/btn_format" 78 79 android: layout_width = "wrap_content" 80 81 android: layout_height = "wrap_content" 82 83 android: text = "formatting"/> 84 85 </LinearLayout> 86 87 88 89 90 </LinearLayout>
Step 2: MainActivity Code: View Code
1 package net. loonggg. chronometer; 2 3 4 5 6 import android. app. activity; 7 8 import android. OS. bundle; 9 10 import android. OS. systemClock; 11 12 import android. OS. vibrator; 13 14 import android. view. view; 15 16 import android. widget. button; 17 18 import android. widget. chronometer; 19 20 import android. widget. chronometer. onChronometerTickListener; 21 22 23 24 25 public class MainActivity extends Activity {26 27 private Vibrator vibrator; 28 29 private Chronometer chronometer; // timing component 30 31 private Button btn_start; 32 33 private Button btn_stop; 34 35 private Button btn_base; 36 37 private Button btn_format; 38 39 40 41 42 @ Override 43 44 public void onCreate (Bundle savedInstanceState) {45 46 super. onCreate (savedInstanceState); 47 48 setContentView (R. layout. main); 49 50 vibrator = (Vibrator) getSystemService (VIBRATOR_SERVICE); // obtain the vibration service 51 52 chronometer = (Chronometer) findViewById (R. id. myChronometer); 53 54 chronometer 55 56. setOnChronometerTickListener (new OnChronometerTickListenerImpl (); // set the ship object 57 58 btn_start = (Button) findViewById (R. id. btn_start); 59 60 btn_stop = (Button) findViewById (R. id. btn_stop); 61 62 btn_base = (Button) findViewById (R. id. btn_base); 63 64 btn_format = (Button) findViewById (R. id. btn_format); 65 66 67 68 69 btn_start.setOnClickListener (new ButtonClickListener (); 70 71 listener (new ButtonClickListener (); 72 73 btn_base.setOnClickListener (new ButtonClickListener ()); 74 75 btn_format.setOnClickListener (new ButtonClickListener (); 76 77} 78 79 80 81 82 public class OnChronometerTickListenerImpl implements // time listening event, monitoring time changes anytime and anywhere 83 84 OnChronometerTickListener {85 86 87 88 89 @ Override 90 91 public void onChronometerTick (Chronometer chronometer) {92 93 String time = chronometer. getText (). toString (); 94 95 if ("00:05 ". equals (time) {// shake the phone 96 97 vibrator after five seconds. vibrate (new long [] {1000, 10,100, 10}, 0); // sets the vibration cycle and whether or not to perform cyclic vibration, change 0 to-1 98 99} 100 101 102} 103 104 105 106 107 108 public class ButtonClickListener implements View if you do not want cyclic vibration. onClickListener {109 110 111 112 113 @ Override114 115 public void onClick (View v) {116 117 switch (v. getId () {118 119 case R. id. btn_start: 120 121 chronometer. start (); // start timing 122 123 break; 124 125 case R. id. btn_stop: 126 127 chronometer. stop (); // stop timing 128 129 break; 130 131 case R. id. btn_base: 132 133 chronometer. setBase (SystemClock. elapsedRealtime (); // reset key 134 135 break; 136 137 case R. id. btn_format: 138 139 chronometer. setFormat ("display time: % s. "); // change the time display format of 140 141 break; 142 143 144 145 146 default: 147 148 break; 149 150 151} 152 153 154 155 156} 157 158}
Step 3: register the permission: <uses-permission android: name = "android. permission. VIBRATE"/>