There is such a working code from the start site android:

package ru.startandroid.develop.p0991servicenotification; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.TextView; public class MainActivity extends Activity { public final static String FILE_NAME = "filename"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = (TextView) findViewById(R.id.tv); Intent intent = getIntent(); String fileName = intent.getStringExtra(FILE_NAME); if (!TextUtils.isEmpty(fileName)) tv.setText(fileName); } public void onClickStart(View v) { startService(new Intent(this, MyService.class)); } public void onClickStop(View v) { stopService(new Intent(this, MyService.class)); } } 

and service:

 package ru.startandroid.develop.p0991servicenotification; import java.util.concurrent.TimeUnit; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class MyService extends Service { NotificationManager nm; @Override public void onCreate() { super.onCreate(); nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } public int onStartCommand(Intent intent, int flags, int startId) { try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } sendNotif(); return super.onStartCommand(intent, flags, startId); } void sendNotif() { // 1-я часть Notification notif = new Notification(R.drawable.ic_launcher, "Text in status bar", System.currentTimeMillis()); // 3-я часть Intent intent = new Intent(this, MainActivity.class); intent.putExtra(MainActivity.FILE_NAME, "somefile"); PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); // 2-я часть notif.setLatestEventInfo(this, "Notification's title", "Notification's text", pIntent); // ставим флаг, чтобы уведомление пропало после нажатия notif.flags |= Notification.FLAG_AUTO_CANCEL; // отправляем nm.notify(1, notif); } public IBinder onBind(Intent arg0) { return null; } } 

Everything works with a bang, but the question is, maybe, who knows how to remove the notification when you close the application?

    2 answers 2

    Override on Activity onDestroy () and add before super.onDestroy ()

     NotificationManager notifManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notifManager.cancelAll(); 

    Found the answer in Google for 46 seconds.

    • Google has let something down. Personally, on all three tablets (3.0, 4.1.2 and 4.2.2), onDestroy not called. Yes, and close all the notifications as something bad. Suddenly, the author needs to close only one specific notification? - Helisia
    • @SuperCreeper, yes, but for some reason I am sure that the author wants to close all notifications (close all, of which only 1). onDestroy should be called. The only exception is when the system requires memory, and it simply kills the process, does not call onDestroy. In this case, the notification will fall off by itself. @ Roslan Rәhmәtullin, but in general it’s right to do exactly the way @SuperCreeper wrote. - Vladyslav Matviienko
    • Thanks, very much helped out - Roslan Rәhmәtullin

    In the service line:

     nm.notify(1, notif); 

    Creates a notification with id 1. I would change it to a larger number, for example 582962 .

    To kill the notification when you exit the application, in the onStop override onStop :

     @Override public void onStop() { NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); nm.cancel(вот тут должен быть id вашего уведомления); super.onStop(); } 
    • @SuperCreeper your method also works, but for some reason after the completion of the application, the notification pops up again ... and besides, as mentioned above, I have only one notification. Perhaps in your code you need to add something, but it is easier for me to copy 1 and paste, for I will be looking for a solution for a long time: D. nubyar i what do you say