I have a Service running in the background and at certain periods of time I create notifications with Notification and when the user clicks the notification, it opens an Activity (SurveyForm). I want to close that Activity when the user presses a button but leave the background Service running. I am calling the finish() method within the Activity but instead of closing it completely, Application is still present in the recent app list.
This is the code that creates the notification.
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);
And the button code is the following:
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();
}
});
Update
I have solved the problem by specifying the next property in the manifest file for the corresponding Activity.
android:excludeFromRecents="true"
I have solved the problem by specifying the excludeFromRecents property in the manifest file for the corresponding Activity.
android:excludeFromRecents="true"
调用finishAndRemoveTask()
而不是finish()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.