In addition to background service notifications, Notification is also commonly used in the following scenarios:
(1) maintain the service. When the system memory is insufficient, the system considers that a background service occupies too long memory and terminates the service to release the memory. For some services, such as playing music, if the system releases resources for the service, the user experience becomes music without sound. For such services, we hope to enjoy a higher priority level and won't be killed by the system.
(2) The user can interact with the service at any time. For example, when playing a music service, you can pause playing music at any time, or select other tracks, or even stop playing music.
To implement the above two points, you can declare that you are foreground in the Service and maintain a notification to indicate the foreground status to the user. You can click the notification in the drop-down list to go to the Service interface, perform interactive operations.
We will extend it in the example of a simulated music playing that inherits the Service class in Android Study Notes (): Service (medium)-inherited Service class.
FakePlayer is omitted from the client code in the original example. The Service Code is as follows:
Public class FakePlayerService extends Service {
Public static final String EXTRA_PLAYLIST = "EXTRA_PLAYLIST ";
Public static final String EXTRA_SHUFFLE = "EXTRA_SHUFFLE ";
Private boolean isPlay = false;
Private static final int policy_fakeplayer_id = 1339;
Public ibinder onbind (intent arg0 ){
Return NULL;
}
Public void ondestroy (){
Stop ();
}
Public int onstartcommand (intent, int flags, int startid ){
String playlist = intent. getstringextra (extra_playlist );
Boolean isshuffle = intent. getbooleanextra (extra_shuffle, false );
Play (playlist, isshuffle );
Return start_not_sticky;
}
Private void play (string playlist, Boolean isshuffle ){
If (! Isplay ){
Isplay = true;
// The process is the same as that used to create a notification in the previous note, but does not need to be triggered through the notification manager. Instead, startforeground (ID,Notify).
// Step 1: Create a notification using notification. Builder () Like the previous one
// Fakeplayer is the activity of two large buttons, that is, the service interface. See the leftmost figure.
Intent I = new intent (this,Fakeplayer. Class);
// Note the flag setting of intent: flag_activity_clear_top: if the activity is already running in the current task, the activity on its front end will be closed, and it becomes the front-end activity. Flag_activity_single_top: if the activity is already running on the frontend, it does not need to be loaded. Setting these two flags is to make one and only one activity (Service Interface) run at the frontend.
I.Setflags (Intent. FLAG_ACTIVITY_CLEAR_TOP|Intent. FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi = PendingIntent. getActivity (this, 0, I, 0 );
Notification mypolicy = new Notification. Builder (this)
. SetSmallIcon (R. drawable. shield)
. SetTicker ("Fake Player:" + playlist)
. SetContentTitle ("Test ")
. SetContentText ("Now Playing: \" Ummmm, Nothing \"")
. SetContentIntent (pi)
. Build ();
// Set the flag of notification, indicating that the notification does not disappear after you click the notification, and the icon is still displayed in the notification bar on the rightmost diagram. This is to ensure that after exiting the activity, there are still icons in the status bar to pull down, click, and enter the activity again.
Mypolicy. Flags | =Notification. FLAG_NO_CLEAR;
// Step 2: startforeground (Int, NotificationSet the service to the foreground status so that the system knows that the service is a user's concern. In the case of low memory, the system will not killed, and provide a notification to indicate that the service is in the foreground status.
StartForeground(NOTIFY_FAKEPLAYER_ID,Mypolicy);
}
}
Private void stop (){
If (isplay ){
Isplay = false;
// Remove the service from the forefround status so that the system can clear it at low memory.
StopForeground(True);
}
}
}
Related links:
My android development articles