As we all know, we generally put the playing logic into the service, so that we can continue playing music in the background. The probability of background services being recycled by the system is relatively low, but this situation does exist.
Front-end services are services that are known to users and are not allowed to be killed by the system when the memory is low. The front-end service must provide a notification to the status bar, which is placed under the title of "Ongoing, this means that the service cannot be removed until the service is terminated or the notification is deleted from the foreground.
For example, a music player service for playing music should be set to run on the front-end, because users have a clear understanding of their operations. The notification in the status bar may indicate the current song, and the user starts an Activity that interacts with the music player.
To run your service on the frontend, call the startForeground () method. This method requires two parameters: an integer that uniquely identifies the notification and a notification to the status bar, for example:
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),System.currentTimeMillis()); Intent notificationIntent = new Intent(this, ExampleActivity.class);PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);notification.setLatestEventInfo(this, getText(R.string.notification_title),getText(R.string.notification_message), pendingIntent); startForeground(ONGOING_NOTIFICATION, notification);
To delete a service from the foreground, call the stopForeground () method. This method requires a Boolean parameter indicating whether to delete the status bar notification. This method does not terminate the service. However, if you terminate a running front-end service, the notification will also be deleted.
Note: The startForeground () and stopForeground () methods are introduced in Android2.0 (API Level 5. To run your service in the old platform version, you must use the previous setForeground () method-For details about how to provide backward compatibility, see the startForeground () method documentation.
The sample code is as follows:
ForgroundService. java
Import java. io. file; import android. app. notification; import android. app. pendingIntent; import android. app. service; import android. content. intent; import android. media. mediaPlayer; import android.net. uri; import android. OS. binder; import android. OS. IBinder; public class ForgroundService extends Service {private static final int icationication_id = 100; private static final Uri mMusicUri = Uri. fromFile (new File ("/Sdcard/sound_file_1.mp3"); private MediaPlayer mMediaPlayer = null; public class ForgroundServiceBinder extends Binder {public void playMusic () {stopCurrentMediaPlayer (); mMediaPlayer = MediaPlayer. create (getApplicationContext (), mMusicUri); mMediaPlayer. start (); String songName = "Test Music"; // assign the song name to songNamePendingIntent pi = PendingIntent. getActivity (getApplicationContext (), 0, New Intent (getApplicationContext (), ForgroundServiceActivity. class), PendingIntent. FLAG_UPDATE_CURRENT); Notification notification Notification = new notification (); Notification. tickerText = "Forground Service"; notification. icon = R. drawable. icon; notification. flags | = Notification. FLAG_ONGOING_EVENT; notification. setLatestEventInfo (getApplicationContext (), "MusicPlayerSample", "Playing:" + songName, pi); start Foreground (icationication_id, notification); // start the Foreground service} public void stopMusic () {stopCurrentMediaPlayer (); stopForeground (true ); // disable front-end service} private ForgroundServiceBinder mBinder = new ForgroundServiceBinder (); @ Overridepublic IBinder onBind (Intent arg0) {return mBinder;} @ Overridepublic void onDestroy () {destroy (); super. onDestroy ();} private void stopCurrentMediaPlayer () {if (mMediaPlayer! = Null) {mMediaPlayer. stop (); mMediaPlayer. release (); mMediaPlayer = null ;}}}
ForgroundServiceActivity. java
import ForgroundService.ForgroundServiceBinder;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.os.Bundle;import android.os.IBinder;import android.view.View;import android.view.View.OnClickListener;public class ForgroundServiceActivity extends Activity {@Overrideprotected void onDestroy() {unbindService(mServiceConnection);super.onDestroy();}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.layout_forground_service);final Intent serviceIntent = new Intent(this, ForgroundService.class);//startService(serviceIntent);bindService(serviceIntent, mServiceConnection, BIND_AUTO_CREATE);}private ServiceConnection mServiceConnection = new ServiceConnection() {@Overridepublic void onServiceConnected(ComponentName name, IBinder binder) {final ForgroundServiceBinder service = (ForgroundServiceBinder) binder;findViewById(R.id.start).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {service.playMusic();}});findViewById(R.id.stop).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {service.stopMusic();}});}@Overridepublic void onServiceDisconnected(ComponentName name) {}};}
Layout_forground_service.xml