Android Tutorial 3 – Adding A Notification To Your Music Service



The third in a series of tutorials on how to code an app that streams music in a service. This tutorial adds a notification that displays when the service is running and is removed when the service ends. Downloadable code available at http://erlangerdigital.com/?page_id=240.

Related Posts

10 Comments

  1. Thanks ill wait for it.

    One Fix that solved most Stateproblems for now.
    (I have MainMenu -> RadioIntent -> RadioService) – my RadioIntent (ur StartIntent) with Button lost State and RadioIntent got more then once initiated.

    I changed the booleanMusicPlaying to static and also

    static Boolean boolMusicPlaying = false;
    static Intent serviceIntent;

    AND at the AndroidManifest the launchMode for my RadioIntent to singleTop.
    Now all works fine.

  2. @killphil75 Very observant. Tutorials 10 and 11 will be dealing with just this — monitoring state and saving persistent data. There are a number of things to cover here. These tutorials should be up within the coming week. Thanks for watching.

  3. @killphil75 If you look at the InitNotification() method toward the end of the Service code, you'll see there is a line in there as follows:"Context context = getApplicationContext();". This line assigns to the variable "context" the value returned by "getApplicationContext()". In other words, this does exactly what you are referring to.

  4. And another Question:

    I got the strange behaviour that my activity lost the State of the Service, any idea how i can check if the Service is running ??? If I close my App, all is fine the stream is running, but if i reopen my Activity the "Playbutton" switch back to play, if I press play, he creates another instance of the player. Just wondering…

  5. Very good tutorial, thank you very much for this. I really enjoy it.
    One question: at the Service-Class .> initNotification you use a context.
    Where did u get this Context ??

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    I used to assign it for myself with
    Context context = getApplicationContext();

    Maybe this is right.

Leave a Reply

Your email address will not be published.

© 2022 Code As Pro - Theme by WPEnjoy · Powered by WordPress