onTaskRemoved() method giving error

Asked

Viewed 162 times

2

I have an Activity that extends Appcompatactivity and in it I have the following method:

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

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

I’ve tried to change the method onDestroy() but without success, because when the application is closed directly in the Task Manager, the application does not go through the onDestroy()

  • What error have you encountered? Does the method simply not work or some exception is thrown? Have any relevant message in logcat?

  • @Roneygomes It does not even compile, it gets a red trace under the @Override with the words Method does not override method from its superclass and, if I take the @Override syntax errors disappear, but in the execution it does not enter inside the method.

2 answers

3

You’re having this problem because the method onTaskRemoved(Intent rootIntent) is not part of AppCompatActivitynor any other class Activity. This is a method that is part of the class Service, as you can check on 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 it at the appropriate time. If you’ve never worked with services before, this guide in English can help you get started.

Also, it is important to note that if you set android:stopWithTask="true" in the declaration by the AndroidManifest.xml then that callback won’t run and the service will end together with the application.

  • I took the responses of this post and managed http://stackoverflow.com/questions/12997800/cancel-notification-on-remove-application-from-multitask-panel

  • Pasta. In that case I suggest you close the question or post your answer here and accept it as a solution.

1


Service class:

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 the 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);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.