Закрыть приложение через кнопку в Notification

Ответить
snubrik
Сообщения: 7
Зарегистрирован: 19 май 2015, 19:46

Закрыть приложение через кнопку в Notification

Сообщение snubrik » 24 июн 2015, 17:12

Коллеги, добрый день!

Пытаюсь полностью закрыть приложение через кнопку в Notification (http://dl2.joxi.net/drive/0003/0975/222 ... 103c0c.jpg)
В фоне работает сервис из которого и вызывается Notification (до запуска сервиса у меня открываются несколько активити (сплешскрин и авторизация)

Код: Выделить всё

Intent intent = new Intent(this, Global.class);

        Intent intentClose = new Intent(this, MainActivity.class);
        intentClose.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intentClose.putExtra("EXIT", true);
        PendingIntent pIntentClose = PendingIntent.getActivity(this, 0, intentClose, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

        Notification notif = new Notification.Builder(this)
                .setContentTitle(Constants.TITLE_SERVICE)
                .setContentText(Constants.BODY_SERVICE)
                .setSmallIcon(R.drawable.icon16)
                .setContentIntent(pIntent)
                .setAutoCancel(true)
                .addAction(R.drawable.switch_off2, Constants.CLOSE_BUTTON_TEXT, pIntentClose).build();

        //notif.setLatestEventInfo(this, Constants.TITLE_SERVICE, Constants.BODY_SERVICE, pIntent);
        //notif.flags |= Notification.FLAG_AUTO_CANCEL;
        startForeground(Constants.FOREGROUND_SERVICE, notif);
В MainActivity вот такой код

Код: Выделить всё

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getIntent().getBooleanExtra("EXIT", false)) {
            finish();
            System.exit(0);
        }
....
}
При клике на кнопку в Notification приложение перезапускается.

Ответить