Because most start-up services do not have to handle multiple requests at the same time (which can be dangerous in multithreaded scenarios), it is a good choice to implement services using the Intentservice class. This experience will teach you how to use Intentservice by inheriting Intentservice output current time.
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent " android:layout_height=" Match_parent "Android: paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" android:paddingbottom= "@dimen/activity_vertical_ Margin " tools:context=" Com.basillee.asus.demo.MainActivity5 "> <button android:layout_width=" Wrap_content " android:layout_height=" wrap_content " android:text=" Current Time " android:id=" @+id/button_ Current_time " android:layout_centervertical=" true " android:layout_centerhorizontal=" true "/></ Relativelayout>
Then we're writing a Currenttimeservice class that inherits Intentservice
Package Com.basillee.asus.demo;import Android.app.intentservice;import Android.content.intent;import Android.text.format.time;import Android.util.log;public class Currenttimeservice extends Intentservice {public Currenttimeservice () { super ("Currenttimeservice"); } @Override protected void onhandleintent (Intent Intent) {Time time=new time (); Time.settonow (); String Currenttime=time.format ("%y-%m-%d%h:%m:%s"); LOG.I ("Currenttimeservice", currenttime); }}
Then we write the following code in the OnCreate method to increase the listener event for the button
Package Com.basillee.asus.demo;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.view.view;import Android.widget.button;public class MainActivity5 extends Activity { @Override protected void OnCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main_activity5); Button button= (button) Findviewbyid (r.id.button_current_time); Button.setonclicklistener (New View.onclicklistener () { @Override public void OnClick (View v) { StartService (New Intent (Mainactivity5.this, Currenttimeservice.class));}} ); }}
For more details, see: http://jingyan.baidu.com/season/48891
Holiday to everyone to send Baidu Baidu red envelopes, we all have to point a little money ha: http://qianbao.baidu.com/hd/red/share/?no=GYVEOtivs8676c3d
Android Intentservice Use