onTaskRemoved () method giving error

2

I have an Activity that extends AppCompatActivity and I have the following method:

 @Override
    public void onTaskRemoved(Intent rootIntent) {
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel(notID);
    }

The goal is to cancel a notification if the user closes the application by the Task Manager. This notification is always displayed when Activity enters the onPause() method.

I have tried to change the onDestroy() method but with no success, because when the application is closed in Task Manager, the application does not pass through onDestroy()

    
asked by anonymous 18.02.2016 / 22:50

2 answers

1

Class of service:

public class KillNotificationsService extends Service {

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

    public class KillBinder extends Binder {
        public final Service service;

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

    @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() {    }

    @Override
    public void onTaskRemoved(Intent rootIntent){
        mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mNM.cancel(NOTIFICATION_ID);
    }
}

Method within Activity

private void serviceNotification(){
    ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className,IBinder binder) {
            ((KillNotificationsService.KillBinder) binder).service.startService(new Intent
                    (MontarTimesActivity.this, KillNotificationsService.class));

            NotificationManager mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            mNM.notify(KillNotificationsService.NOTIFICATION_ID, notification().getNotification());
        }

        public void onServiceDisconnected(ComponentName className) {
        }
    };
    bindService(new Intent(MontarTimesActivity.this, KillNotificationsService.class), mConnection, Context.BIND_AUTO_CREATE);
}
    
22.02.2016 / 12:56
3

You are experiencing this problem because the onTaskRemoved(Intent rootIntent) method is not part of AppCompatActivity or any other Activity class. This is a method that is part of the Service class, as you can see in documentation . Therefore, to be able to do what you want, you will need to implement your own service, register it in your application and call you at the appropriate time. If you've never worked with services before, this guide in Portuguese can help you start.

In addition, it is important to note that if you set android:stopWithTask="true" in the service declaration to AndroidManifest.xml then this callback will not run and the service will end with the application.

    
19.02.2016 / 14:30