簡體   English   中英

在Activity上調用finish()時從最近的應用程序列表中排除該應用程序

[英]Excluding the application from recent app list on invoking finish() on Activity

我有一個在后台運行的服務,在某些時間段內,我使用Notification創建通知,並且當用戶單擊通知時,它會打開一個活動(SurveyForm)。 我想在用戶按下按鈕但關閉后台服務運行時關閉該活動。 我在Activity中調用了finish()方法,但不是完全關閉它,而是在最近的應用程序列表中仍然存在Application。

這是創建通知的代碼。

Intent notificationIntent = new Intent(this, SurveyForm.class);
    notificationIntent.setAction(Constants.SURVEY_ACTION);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Notification notification = new NotificationCompat.Builder(this)
            .setContentTitle("Survey")
            .setTicker("New Survey")
            .setContentText("Please, answer the survey.")
            .setSmallIcon(R.drawable.survey_notification_icon)
            .setSound(alarmSound)
            .setLights(0xff00ff00, 1000, 1000)
            .setContentIntent(resultPendingIntent)
            .setOngoing(true)
            .build();
    notification.defaults = Notification.DEFAULT_VIBRATE;

    mNotificationManager.notify(Constants.SURVEY_NOTIFICATION_ID, notification);

按鈕代碼如下:

bSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
             //Close the notification
             nm.cancel(Constants.SURVEY_NOTIFICATION_ID);

             //Close this Activity
             finish();
        }
    });

更新

我已經通過在清單文件中為相應的Activity指定下一個屬性來解決了這個問題。

機器人:excludeFromRecents = “真”

我已經通過在清單文件中為相應的Activity指定excludeFromRecents屬性來解決了這個問題。

機器人:excludeFromRecents = “真”

調用finishAndRemoveTask()而不是finish()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM