Service applications in Android

Source: Internet
Author: User

In Android development, when you need to create a program running in the background, you need to use the Service. Service can be divided into two types: infinite life and limited life. Note that the Service and Activities are different (in simple terms, it can be understood as the difference between the background and the foreground). For example, if you need to use the Service, you need to call startService (), startService () is used to call the OnCreate () and onStart () methods in the Service to start a backend Service.
The process of starting a Service is as follows: context. startService ()-> onCreate ()-> onStart ()-> Service running where onCreate () can initialize some services, while onStart () starts the Service.
The process of stopping a Service is as follows: context. stopService () |-> onDestroy ()-> Service stop
The following example shows a small example of using the background service to play music. Click start to run the service and then stop the service.
ServicesDemo. java (an Activity)

Package com. android. myservice;

Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;

Public class ServiceDemo extends Activity implements OnClickListener {
Private static final String TAG = "ServiceDemo ";
Button buttonStart, buttonStop;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

ButtonStart = (Button) findViewById (R. id. buttonStart );
ButtonStop = (Button) findViewById (R. id. buttonStop );

ButtonStart. setOnClickListener (this );
ButtonStop. setOnClickListener (this );
}

Public void onClick (View src ){
Switch (src. getId ()){
Case R. id. buttonStart:
Log. I (TAG, "onClick: starting service ");
StartService (new Intent (this, MyService. class ));
Break;
Case R. id. buttonStop:
Log. I (TAG, "onClick: stopping service ");
StopService (new Intent (this, MyService. class ));
Break;
}
}
}
In addition, the Service must be declared in Manifest: (AndroidManifest. xml)
<? Xml version = "1.0" encoding = "UTF-8"?>

<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. android. myservice">
<Application android: label = "@ string/app_name">
<Activity android: name = ". ServiceDemo" android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
<Service android: enabled = "true" android: name = ". MyService"/>
</Application>
</Manifest>

Define Service (MyService. java)
Package com. android. myservice;

Import android. app. Service;
Import android. content. Intent;
Import android. media. MediaPlayer;
Import android. OS. IBinder;
Import android. util. Log;
Import android. widget. Toast;

Public class MyService extends Service {
Private static final String TAG = "MyService ";
MediaPlayer player;

@ Override
Public IBinder onBind (Intent intent ){
Return null;
}

@ Override
Public void onCreate (){
Toast. makeText (this, "My Service created", Toast. LENGTH_LONG). show ();
Log. I (TAG, "onCreate ");

Player = MediaPlayer. create (this, R. raw. braincandy );
Player. setLooping (false );
}

@ Override
Public void onDestroy (){
Toast. makeText (this, "My Service Stoped", Toast. LENGTH_LONG). show ();
Log. I (TAG, "onDestroy ");
Player. stop ();
}

@ Override
Public void onStart (Intent intent, int startid ){
Toast. makeText (this, "My Service Start", Toast. LENGTH_LONG). show ();
Log. I (TAG, "onStart ");
Player. start ();
}
}
Main. xml is in the layout folder.
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: gravity = "center">
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content" android: text = "@ string/services_demo" android: gravity = "center" android: textSize = "20sp" android: padding = "20dp"/>
<Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/buttonStart" android: text = "@ string/start"> </Button>
<Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "@ string/stop" android: id = "@ + id/buttonStop"> </Button>
</LinearLayout>

The value folder is strings. xml.
<? Xml version = "1.0" encoding = "UTF-8"?>

<Resources>
<String name = "start"> Start </string>
<String name = "stop"> Stop </string>
<String name = "services_demo"> Service Demo </string>
</Resources>

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.