Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6826713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:10:49+00:00 2026-05-26T22:10:49+00:00

I’m currently building an app that will play sounds. When a user starts a

  • 0

I’m currently building an app that will play sounds. When a user starts a sound, I display a status bar notification with this code:

notificationManager = (NotificationManager) activity.getSystemService(Activity.NOTIFICATION_SERVICE);

Intent intent = new Intent(activity, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);

notification = new Notification();
notification.icon = android.R.drawable.stat_notify_sync;
notification.flags |= Notification.FLAG_ONGOING_EVENT;

String title = activity.getString(R.string.notification_title);
String text = activity.getString(R.string.notification_text);

notification.setLatestEventInfo(activity, title, text, pendingIntent);

This is working fine. Later, if the user stops all sound, I use notificationManager.cancel to remove the notification, and again, this is working fine.

My problem is that I want to handle the case of someone using a task killer app. If someone uses one to kill my app, I’d like the notification icon to be removed from the status bar. I know that a lot of users like to use those kind of apps and I don’t want some to complain that they can’t “close” my app!

So far, I have not been able to detect when the app is about to exit. I’ve tried this:

Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
{
  @Override
  public void uncaughtException(Thread thread, Throwable ex)
  {
    notificationManager.cancel(NOTIFICATION_ID);
  }
});

And also this:

Runtime.getRuntime().addShutdownHook(new Thread()
{
  @Override
  public void run()
  {
     notificationManager.cancel(NOTIFICATION_ID);              
  }
});

But this is not working. When my app gets killed, the notification icon is still there. To kill my app, I’ve been using an app called “Advanced Task Killer”. I’ve also test by killing my process within DDMS.

I’ve noticed that the native media player is doing that. If a music is playing and you kill the music service, the icon will disappear.

So, is there a way to detect when an application is about to exit so that I can remove the notification icon? Or is there a way for the notification icon to be removed automatically if my app is killed?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:10:50+00:00Added an answer on May 26, 2026 at 10:10 pm

    There are two types of icons that appear in the Notification bar:

    1. Notification, sent by an application or the system
    2. Foreground services

    You’re using the first type. It’s a notification sent by the application (or system) and it’s goal is to deliver some information to the user. Examples: New mail arrived, Low storage space, new comment on my post.
    These are all informations that the user theoretically wants to check and it should stay up there until the user taps it to see the details (if there are any) or decides to remove them removing all the notifications left.

    I’ll start talking about foreground services with a quote from the documentation

    A foreground service is a service that’s considered to be something
    the user is actively aware of and thus not a candidate for the system
    to kill when low on memory. A foreground service must provide a
    notification for the status bar, which is placed under the “Ongoing”
    heading, which means that the notification cannot be dismissed unless
    the service is either stopped or removed from the foreground.

    For example, a music player that plays music from a service should be
    set to run in the foreground, because the user is explicitly aware of
    its operation. The notification in the status bar might indicate the
    current song and allow the user to launch an activity to interact with
    the music player.

    This is the case of the Music app you mentioned in your question. The player part, inside of the Music app, is implemented as a foreground service. When the user selects a song and plays it, through the app UI, the service is started (and the icon comes up in the Notification Bar) and song reproduction starts and it can go on independently from the UI itself. When the song ends or the user stops the reproduction, the icon will disappear as the service is(should be) stopped.

    To learn more about how to implement a foreground service, refer to link above (just click the word “documentation”)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I am currently running into a problem where an element is coming back from
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.