AlarmManager
을 사용하는 알림이 특정 날짜에 설정되었다고 가정 해 봅니다. 표시하기 전에 단추 누르기로 해당 알림을 삭제하려면 어떻게해야합니까? 내가 실수하지 않는다면 NotificationManager.cancel(id)
메서드는 현재 표시되고있는 알림을 취소합니다. 출현하기 전에 삭제하고 싶다면 어떻게해야합니까? arraylist에서 항목을 제거하는 방법이나 데이터베이스에서 행을 삭제하는 방법과 비슷합니다.Android - 표시되기 전에 알림 삭제 또는 취소
예 내가 id,name
와 Person
객체를 가지고 매번 내가 데이터베이스에 Person
을 추가합니다 Notification
는 지정된 날짜에 설정됩니다. 여기
public void setNotification(Calendar calendar,Person person){
AlarmManager alertManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent intent = new Intent();
intent.setAction("android.media.action.DISPLAY_NOTIFICATION");
intent.addCategory("android.intent.category.DEFAULT");
intent.putExtra("myTag",person);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, person.getId(), intent, PendingIntent.FLAG_ONE_SHOT);
alertManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
내 경보 수신기 클래스 (예 내가 DAO 디자인 패턴을 구현하고있어 내가 가지고) 싱글 클래스 인 후자 PersonDao와 PersonDaoImpl :
public class AlarmReceiver extends BroadcastReceiver {
final static String GROUP_KEY = "GROUP_KEY";
@Override
public void onReceive(Context context, Intent intent) {
//retrieve info:
PersonDAO personDao = PersonDAOImpl.getInstance(context);
Person person = (Person)intent.getSerializableExtra("myTag");
String name = person.getName();
long[] vibrationPattern = {0, 300, 0, 300};
Intent notificationIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(ViewDetails.class);
stackBuilder.addNextIntent(notificationIntent);
int requestCode = person.getId();
PendingIntent pendingIntent = stackBuilder.getPendingIntent(requestCode, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
Notification notification = builder.setContentTitle("Deadline due today")
.setContentText("name: " + name)
.setTicker(name+"'s debt is due today")
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pi)
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(requestCode, notification);
}
예 경보 리시버에 설정 한 날짜는 2016 오후 3시 년 9 월 12 살. 현재 날짜는 Septermber 10 2016입니다. 내가하고 싶은 일은 데이터베이스에있는 사람을 삭제할 때 통지가 삭제되거나 취소 된 것입니다. 따라서 2016 년 9 월 12 일 오후 3시에 더 이상 경보가 표시되지 않습니다. 당신은 당신이 AlarmManager
를 통해 예약 뭔가를 취소하려면
"특정 날짜에 알림이 설정되었습니다."- 이는 알림의 기능이 아닙니다. 이 작업을 어떻게 수행하는지 보여주는 [mcve]를 제공 할 수 있습니다. – CommonsWare
알람 관리자 사용 @CommonsWare – Andre
@Andre 코드 예제를 작성하십시오. – mixel