Android Service and androidservice

Source: Internet
Author: User

Android Service and androidservice

 

Service is one of the four main components in the Android system. Unlike Activity, Service cannot interact with users. It is a service program running in the background without a visual interface.
1. The service in Android does not have a user interface. It runs in the system and is not easily noticed by users. You can use it to develop programs such as monitoring. Service development is relatively simple, as follows:
Step 1: Inherit the Service class
Public class MyService extends Service {}
Step 2: configure the service on the <application> node in the AndroidManifest. xml file:
<Service android: name = ". MyService"/>
The service cannot run on its own. You must call the Context. startService () or Context. bindService () method to start the service. Both methods can start the Service
2. Like Activity, Service also has a process from startup to destruction, but the process of Service is much simpler than Activity. The process from Service startup to destruction only goes through the following three stages:
Create public void onCreate ();

Start Service public void onStart (Intent intent, int startId );

Destroy the public void onDestroy () service ();

 

 

MyService. javapackage com. example. service; import android. app. service; import android. content. intent; import android. OS. IBinder; public class MyService extends Service {@ Overridepublic IBinder onBind (Intent intent) {// TODO Auto-generated method stubreturn null;} @ Overridepublic void onCreate () {// TODO Auto-generated method stubSystem. out. println ("### onCreate ###") ;}@ Overridepublic void onDestroy () {// TODO Auto-generated method stubSystem. out. println ("### onDestroy ###") ;}@ Overridepublic int onStartCommand (Intent intent, int flags, int startId) {// TODO Auto-generated method stubSystem. out. println ("### onStartCommand ###"); return Service. START_CONTINUATION_MASK; // continue execution }}


 

 

MainActivity.javapackage com.example.service;import android.os.Bundle;import android.app.Activity;import android.content.DialogInterface;import android.content.Intent;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity {private Button start=null;private Button stop=null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);start=(Button)findViewById(R.id.start);stop=(Button)findViewById(R.id.stop);this.start.setOnClickListener(new StartOnClickListenerImpl());this.stop.setOnClickListener(new StartOnClickListenerImpl());}private  class StartOnClickListenerImpl implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubMainActivity.this.startService(new Intent(MainActivity.this,MyService.class));}private  class StoptOnClickListenerImpl implements OnClickListener{@Overridepublic void onClick(View v) {// TODO Auto-generated method stubMainActivity.this.startService(new Intent(MainActivity.this,MyService.class));}}}}


Do not forget AndroidManifest. xml.


 


Question about the android service?

You can add a timer in the onstart method so that it can be executed every several seconds.
Timer mTimer = new Timer ();
MTimer. schedule (new TimerTask (){
@ Override
Public void run (){
// The operation to be executed
}
}, 5*1000, 10*1000); // After the schedule () method is called, wait for 5s to execute the run () method for the first time, and then run it every 10 seconds.

// Stop the timer
MTimer. cancel ();

How to enable Service in Android and disable Service

You can set events in the activity. Use intent when the event starts
Intent intent = new Intent (MainActivity. this, ServiceDemo. class );
Switch (v. getId ())
{Case R. id. startBtn:
StartService (intent); Start
Break;
Case R. id. stopBtn:
StopService (intent); Disabled
Break;
Default:
Break;

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.