Android Intentservice Use

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.