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