Android Learning Note (52): Service Services (middle)-inheriting the service class

Source: Internet
Author: User

The service that is triggered by the command is implemented through the inheriting class of intentservice, or it can be implemented directly through the inheritance class of the service. In the example of Intentservice, we added the StopService () method to the experiment. In practical application, Intentservice is often used for one-time operation, automatic end of the situation, do not need to stop manual intervention. For a stopped, long (or no-limit) operation that requires manual intervention, you can directly inherit the service's way, such as music playback. Intentservice is also an inheritance class for the service.

Inheriting the service class

In this example, we simulate music playback, providing two parameters, one for the music list and one for the shuffle logo.

Unlike the previous example, the service does not end automatically and must be user-intervened. For example, playing music or audiobook, when the user left appcliation, the music can continue to play, the service will continue, not stop, until the user actively stop the service, even if the music is finished, the mode is not looping, when all the music played, Service still exists until the user dominates StopService () or the service itself stopself (). The example is a mock example that does not involve real music playback.

Client code

public class ServiceTest2 extends activity{
... ...
//For the client, similar to the last example, with intent as the command, this example passes a parameter of two, through StartService () to start the service.
private void Startplayer () {
Intent i =new Intent (This,serviceplayer.class);
I.PutExtra(Serviceplayer.extra_playlist, "PLAYLIST");
I.PutExtra(Serviceplayer.extra_shuffle, True);
StartService(i);
}
//Stop service with user intervention. In the example, we do not use the intent to create the service, because the activity life cycle may have ended, intent has been garbage collected, we re-opened the activity, in the new activity, do not need to open the service can be directly stopped.
private void Stopplayer () {
StopService (New Intent (This,serviceplayer.class));
}
}

Service-side code

public class Serviceplayer extends service{
public static final String extra_playlist = "Com.wei.android.learning.EXTRA_PLAYLIST";
public static final String extra_shuffle = "Com.wei.android.learning.EXTRA_SHUFFLE";
Private Boolean isplaying = false;
  The mode of BIND service will be learned later
Public IBinder Onbind (Intent arg0){
return null;
}
//received a client trigger, and if the service has not been started, execute OnCreate () before service initialization is processed.
Public void OnCreate (){
Super.oncreate ();
}
//At the time of receipt of StopService (), we will handle the termination of the service, such as stopping the running of the background thread and so on.
Public void OnDestroy (){
Playstop ();
Super.ondestroy ();
}
When the system is in memory, the service will be required to be terminated, and we are here to handle the shutdown service, such as maintaining the relevant State, such as the service needs to maintain a specific state, there is no need to override this method.
public void Onlowmemory ()
{
Super.onlowmemory ();
}
    //received a client request when triggered, because Onstartcommand () running in the main thread, will be the initialization of the service, and open the background thread to run related processing, specifically participate in thread learning notes, this example only examines service-related content. The return value tells the system what will happen if the service's process is killed: Start_sticky indicates that the service will return to the starting state, as Onstartcommand () is called, but intent will not be re-sent; Start_redeliver_ Intent indicates that the system is required to resend the intent, that is, the service will re-trigger execution Onstartcommand (), start_not_sticky indicates no intervention, the service stops, and waits for a new command to fire.
public int Onstartcommand (Intent Intent, int flags, int startid){
Showinfo ("Onstartcommand");
String playlist = Intent.getstringextra (extra_playlist);
Boolean suffle = Intent.getbooleanextra (Extra_shuffle, false);
Playstart (Playlist,suffle);
return start_not_sticky;
}
private void Playstart (String playlist, Boolean suffle) {
if (! isplaying) {
IsPlaying = true;
}
}
private void Playstop () {
if (isplaying) {
Showinfo ("Go to stop!");
IsPlaying = false;
}
}
}

If there are three clients that pass StartService (), only one service is started. Only when the first command is issued, the System Discovery service does not start, the service starts, and the rest of the direct trigger Onstartcommand (). by StartService () it is not just sending a command, but telling the system service to keep running until it notifies it that the service is stopped.

RELATED Links: My Android development related articles

Android Learning Note (52): Service Services (middle)-inheriting the service class

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.