In android applications, there is a class without UI (android. app. Service) -- Service. To put it simply, the Service is a background process (background Program). Through the background program, you can implement some functions that do not require UI, such as playing background music.
The following is a routine for playing background music:
On the basis of the previous project, add the music playing function to the Activity.
Add a New Class yypService (File-> New-> Class) to the project ):
[Java] </pre> <pre name = "code" class = "java"> import android. app. Service;
Import android. content. Intent;
Import android. OS. IBinder;
Public class yypService extends Service {
@ Override
Public IBinder onBind (Intent intent ){
// TODO Auto-generated method stub
Return null;
}
}
</Pre> <pre name = "code" class = "java"> import android. app. Service;
Import android. content. Intent;
Import android. OS. IBinder;
Public class yypService extends Service {
@ Override
Public IBinder onBind (Intent intent ){
// TODO Auto-generated method stub
Return null;
}
}
The yypService class inherits android. app. Service. Several important concepts about Service are as follows:
1. The Service object is executed in the form of separated process. This indicates that the Service and UI (Activity) are not executed in the same process, but are executed in different processes.
2. The Android Application starts and stops the Service in the Activity.
3. The override onStart () method is started in the Service to execute the desired background function.
4. When the onDestroy () method is stopped, the background function is stopped.
The specific implementation of the Service is as follows:
[Java] package com. android;
Import java. io. IOException;
Import android. app. Service;
Import android. content. Intent;
Import android. media. MediaPlayer;
Import android. OS. IBinder;
Public class yypService extends Service {
Private MediaPlayer mp;
@ Override
Public void onStart (Intent intent, int startId ){
// TODO Auto-generated method stub
// Start playing music
Mp. start ();
// Event processing after music playing
Mp. setOnCompletionListener (new MediaPlayer. OnCompletionListener (){
Public void onCompletion (MediaPlayer mp ){
// TODO Auto-generated method stub
// Loop playback
Try {
Mp. start ();
} Catch (IllegalStateException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
});
// Handle an error when playing music
Mp. setOnErrorListener (new MediaPlayer. OnErrorListener (){
Public boolean onError (MediaPlayer mp, int what, int extra ){
// TODO Auto-generated method stub
// Release resources
Try {
Mp. release ();
} Catch (Exception e ){
E. printStackTrace ();
}
Return false;
}
});
Super. onStart (intent, startId );
}
@ Override
Public void onCreate (){
// TODO Auto-generated method stub
// Initialize music Resources
Try {
// Create a MediaPlayer object
Mp = new MediaPlayer ();
// Save the music in res/raw/xingshuffles. The {public static final int xingshu = 0x7f040000;} is automatically generated in R. java ;}
Mp = MediaPlayer. create (yypService. this, R. raw. xingshu );
// Use MediaPlayer. prepeare ()
Mp. prepare ();
} Catch (IllegalStateException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Super. onCreate ();
}
@ Override
Public void onDestroy (){
// TODO Auto-generated method stub
// Stop playing music and release resources when the service is stopped
Mp. stop ();
Mp. release ();
Super. onDestroy ();
}
@ Override
Public IBinder onBind (Intent intent ){
// TODO Auto-generated method stub
Return null;
}
}
Package com. android;
Import java. io. IOException;
Import android. app. Service;
Import android. content. Intent;
Import android. media. MediaPlayer;
Import android. OS. IBinder;
Public class yypService extends Service {
Private MediaPlayer mp;
@ Override
Public void onStart (Intent intent, int startId ){
// TODO Auto-generated method stub
// Start playing music
Mp. start ();
// Event processing after music playing
Mp. setOnCompletionListener (new MediaPlayer. OnCompletionListener (){
Public void onCompletion (MediaPlayer mp ){
// TODO Auto-generated method stub
// Loop playback
Try {
Mp. start ();
} Catch (IllegalStateException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
});
// Handle an error when playing music
Mp. setOnErrorListener (new MediaPlayer. OnErrorListener (){
Public boolean onError (MediaPlayer mp, int what, int extra ){
// TODO Auto-generated method stub
// Release resources
Try {
Mp. release ();
} Catch (Exception e ){
E. printStackTrace ();
}
Return false;
}
});
Super. onStart (intent, startId );
}
@ Override
Public void onCreate (){
// TODO Auto-generated method stub
// Initialize music Resources
Try {
// Create a MediaPlayer object
Mp = new MediaPlayer ();
// Save the music in res/raw/xingshuffles. The {public static final int xingshu = 0x7f040000;} is automatically generated in R. java ;}
Mp = MediaPlayer. create (yypService. this, R. raw. xingshu );
// Use MediaPlayer. prepeare ()
Mp. prepare ();
} Catch (IllegalStateException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Catch (IOException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Super. onCreate ();
}
@ Override
Public void onDestroy (){
// TODO Auto-generated method stub
// Stop playing music and release resources when the service is stopped
Mp. stop ();
Mp. release ();
Super. onDestroy ();
}
@ Override
Public IBinder onBind (Intent intent ){
// TODO Auto-generated method stub
Return null;
}
}
So far, a complete service is generated, and then the service is started in the Activity.
Modify AndroidManifest. xml
In the Package Explorer window, find the Information Description file of the current Android project, named AndroidManifest. xml. This is a file used to describe the "overall information" of the Android application. Each Android Application project has one. Here, the purpose of modifying Androidmanifest. xml is to "add an Android Application to a Service category" so that you can drive the Service.
[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. android"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Uses-sdk android: minSdkVersion = "10"/>
<Application
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">
<Activity
Android: name = ". WebTestActivity"
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: name = ". yypService"
Android: exported = "true"
Android: process = ": remote">
</Service>
</Application>
</Manifest>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "com. android"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Uses-sdk android: minSdkVersion = "10"/>
<Application
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name">
<Activity
Android: name = ". WebTestActivity"
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: name = ". yypService"
Android: exported = "true"
Android: process = ": remote">
</Service>
</Application>
</Manifest>
After the configuration, add the startup Service Code Service-startService () in the Activity ().
Add the following code to OnCreate:
[Java] Intent intent = new Intent (WebTestActivity. this, yypService. class );
StartService (intent );
Intent intent = new Intent (WebTestActivity. this, yypService. class );
StartService (intent );
There is a method in the Activity class called startService:
StartService (Intent service)
Call startService () to start a Service. However, the startService () parameter is an "Intent" type and is not the name of the class to be started. "Intent" is a class similar to "Event". If you haven't made any in-depth research on Intent yet, consider it as an "Event" first?
Now, you can play background music in the Activity, but there is a small problem: when the Activity has been suspended or destroyed, the background music is still playing, this also indicates that the Service and Activity are two different processes. We accept the end and let the Activity stop playing the background music during OnStop, and reload the OnStop of the Activity:
[Java] @ Override
Protected void onStop (){
// TODO Auto-generated method stub
Intent intent = new Intent (WebTestActivity. this, yypService. class );
StopService (intent );
Super. onStop ();
}
@ Override
Protected void onStop (){
// TODO Auto-generated method stub
Intent intent = new Intent (WebTestActivity. this, yypService. class );
StopService (intent );
Super. onStop ();
}
Note: Tips for using eclipse to quickly complete the Heavy Load Code (shortcut: Alt + Shift + s-> Override/implement Mothods ).
So far, a complete background music playing function has been completed.
From Young's column