Android IntentService, androidintent

Source: Internet
Author: User

Android IntentService, androidintent

Because most startup services do not have to process multiple requests at the same time (which is dangerous in multi-threaded scenarios), it is a good choice to use the IntentService class to implement the service. This experience will teach you how to use IntentService by inheriting the IntentService output 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/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/activity_vertical_margin" android: paddingBottom = "@ dimen/plugin" 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 compile a CurrentTimeService class to inherit the 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 add a 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

For the holidays to send Baidu red envelopes, We have to small money ha: http://qianbao.baidu.com/hd/red/share? No = GYVEOtivs8676c3d

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.