First define the Activity_main.xml file
The code is as follows:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:orientation=" vertical " android:layout_width=" Fill_parent " android:layout_height= "Fill_parent" ><chronometer android:id= "@+id/mychronometer" android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content"/><linearlayout android:orientation= " Horizontal " android:layout_width=" fill_parent " android:layout_height=" fill_parent "> <Button android:id= "@+id/butstart" android:text= "Start timing" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/> <button android:id= "@+id/butstop" android:text= " Stop timing " android:layout_width=" wrap_content " android:layout_height=" Wrap_content "/></linearlayout ></LinearLayout>
Then rewrite the Mainactivity class
The code is as follows:
Package Com.example.myclock;import Android.os.bundle;import Android.os.systemclock;import android.os.Vibrator; Import Android.app.activity;import android.app.service;import Android.view.menu;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.chronometer;import Android.widget.chronometer.onchronometerticklistener;public class Mainactivity extends Activity {private chronometer Mychronometer = null;private Button Butstart = null;private button Butstop = null;private Vibrator vibrator = null; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); This.mychronometer = (chronometer) Super.findviewbyid (r.id.mychronometer); This.vibrator = (vibrator) super.getapplication (). Getsystemservice (Service.vibrator_service); This.butstart = (Button) Super.findviewbyid (R.id.butstart); This.butstop = (Button) super.fiNdviewbyid (R.id.butstop); This.butStart.setOnClickListener (New Onclicklistenerlmplstart ()); This.butStop.setOnClickListener (New Onclicklistenerlmplstop ()); This.myChronometer.setFormat ("Current time:%s.") "); This.myChronometer.setOnChronometerTickListener (New Onchronometerticklistenerlmpl ()); } Private class Onchronometerticklistenerlmpl implements onchronometerticklistener{public void Onchronometertick (Ch Ronometer chronometer) {String time = Chronometer.gettext (). toString (). ReplaceAll ("[^ (\\d{2}:\\d{2})]", ""); if ("01:00". Equals (Time)) {MainActivity.this.vibrator.vibrate (new long[]{1000,10,1000,100},0); }}} Private class Onclicklistenerlmplstart implements onclicklistener{public void OnClick (view view) {Ma InActivity.this.myChronometer.start (); }} Private class Onclicklistenerlmplstop implements onclicklistener{public void OnClick (view view) {Mainacti Vity.this.myChronometer.stop (); Mainactivity.this. Mychronometer.setbase (Systemclock.elapsedrealtime ()); MainActivity.this.vibrator.cancel (); }} @Override public boolean Oncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.activity_ma in, menu); return true; }}
Finally rewrite the Androidmainfest.xml file
The code is as follows:
<manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= "Com.example.myclock " Android:versioncode= "1" android:versionname= "1.0" > <uses-sdk android:minsdkversion= " android:targetsdkversion= "/> <application android:icon=" @drawable/ic_launcher " android: Label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:name= ". Mainactivity " android:label=" @string/title_activity_main "> <intent-filter> <action Android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER "/> </intent-filter> </activity> </application><uses-permission Android:name= "Android.permission.VIBRATE"/></manifest>
Operating effects such as:
Android chronometer