[Android learning] Service for Android Services (1) -- Service preliminary

Source: Internet
Author: User

Service is the most similar to Activity among the four Android components, and it has its own life cycle completely. However, the difference with Activity is that the Service has been running in the background and has no user interface, therefore, it is impossible to interact with users. The selection criteria of Activity and Service in a program are as follows: if a program component needs to present a certain interface to the user during running time, or the program needs to interact with the user, the Activity needs to be used, otherwise, you should consider the Service.

Create a Service:

 

package com.ye_yun_lin.service;import android.app.Service;import android.content.Intent;import android.os.IBinder;public class TestService extends Service{@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onCreate() {super.onCreate();System.out.println("Service onCreat");}@Overridepublic void onDestroy() {super.onDestroy();System.out.println("Service onDestroy");}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {System.out.println("Service onStartCommand");return super.onStartCommand(intent, flags, startId);}}

Configure Service:

 

 

        
             
                              
          
 

Start and Stop a Service:

 

 

package com.ye_yun_lin.service;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.text.style.ClickableSpan;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {private Button startButton;private Button destroyButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);System.out.println("Activity onCreat");startButton=(Button)findViewById(R.id.startbutton);destroyButton=(Button)findViewById(R.id.destroybutton);startButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent();intent.setClass(MainActivity.this, TestService.class);startService(intent);}});destroyButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent();intent.setClass(MainActivity.this, TestService.class);stopService(intent);}});}}


 

After you click the Start Service button three times, the following message is displayed:

It can be seen that the onCreate () method will be called back each time a Service is created, and the onStart () method will be called back every time a Service is started-The onCreate method will not be called back when multiple existing Service components are started, however, the onStartCommand () method will be called back each time it is started.

The above is a simple application of the Service component. Although this Service has nothing to do, if you want this Service component to do something, then in onCreat () or onStartCommand () define the relevant business code in the method.

In the next article, we will introduce the Service in depth.

Related Article

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.