2017-02-11 37 views
1

我有一個服務在後臺運行,並且在某些時候我使用Notification創建通知,當用戶單擊通知時,它會打開一個Activity(SurveyForm )。我想在用戶按下按鈕但關閉後臺服務時關閉該Activity。我在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(); 
     } 
    }); 

更新

我已經在相應的活動清單文件指定一個屬性解決了這個問題。

機器人:excludeFromRecents =「真」

+1

你所說的 「最小化」 是什麼意思? Android沒有「最小化」應用程序的概念。 – Karakuri

+1

@Karakuri通過最小化我的意思是應用程序被隱藏,但仍然出現在最近的應用程序列表中。 – Enrique

+0

@Enrique,請將解決方案作爲問題的答案發布,然後接受相同的答案。它會以更好的方式幫助其他人,也可以爲你贏得積分。 –

回答

2

我已經在相應的活動清單文件中指定excludeFromRecents特性解決了這個問題。

機器人:excludeFromRecents = 「真」

相關問題