0

I have a simple app that downloads a file from the internet using a service showing the progress in a progress dialog and also in an ongoing notification. My problem is how to remove the notification when the user stops the download by force closing the app (for example by long pressing the home button and by clearing all the recent apps list).

I tried with this:

@Override
protected void onDestroy() {


    Toast.makeText(getApplicationContext(), "ADDIO", Toast.LENGTH_SHORT).show();

    NotificationManager nm =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    nm.cancelAll();

    super.onDestroy();

}

but it doesn't work.

Please help

asdf
  • 97
  • 1
  • 10

1 Answers1

0

You could start a service from your activity.

Step 1 create a service that kills Simple one. It just kills a notification on create and has his special Binder.

public class KillNotificationsService extends Service {

    public class KillBinder extends Binder {
        public final Service service;

        public KillBinder(Service service) {
            this.service = service;
        }

    }

    public static int NOTIFICATION_ID = 666;
    private NotificationManager mNM;
    private final IBinder mBinder = new KillBinder(this);

    @Override
    public IBinder onBind(Intent intent) {
            return mBinder;
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
            return Service.START_STICKY;
    }
    @Override
    public void onCreate() {
            mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            mNM.cancel(NOTIFICATION_ID);
    }
}

Step2: Add it to your manifest: Add it somewhere inbetween your <application> tags.

<service android:name="KillNotificationsService"></service>

Step3: Always create the Service before firing the notification, and use the static notificationid.

ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className,
            IBinder binder) {
        ((KillBinder) binder).service.startService(new Intent(
                MainActivity.this, KillNotificationsService.class));
        Notification notification = new Notification(
                R.drawable.ic_launcher, "Text",
                System.currentTimeMillis());
        Intent notificationIntent = new Intent(MainActivity.this,
                Place.class);
        PendingIntent contentIntent = PendingIntent.getActivity(
                MainActivity.this, 0, notificationIntent, 0);
        notification.setLatestEventInfo(getApplicationContext(),
                "Text", "Text", contentIntent);
        NotificationManager mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mNM.notify(KillNotificationsService.NOTIFICATION_ID,
                notification);
    }

    public void onServiceDisconnected(ComponentName className) {
    }

};
bindService(new Intent(MainActivity.this,
        KillNotificationsService.class), mConnection,
        Context.BIND_AUTO_CREATE);

It might take a little time until service is restarted (1-5 sec), but it will eventually start and kill the notification.

harshithdwivedi
  • 1,411
  • 16
  • 37