2016-09-09 2 views
0

AlarmManager을 사용하는 알림이 특정 날짜에 설정되었다고 가정 해 봅니다. 표시하기 전에 단추 누르기로 해당 알림을 삭제하려면 어떻게해야합니까? 내가 실수하지 않는다면 NotificationManager.cancel(id) 메서드는 현재 표시되고있는 알림을 취소합니다. 출현하기 전에 삭제하고 싶다면 어떻게해야합니까? arraylist에서 항목을 제거하는 방법이나 데이터베이스에서 행을 삭제하는 방법과 비슷합니다.Android - 표시되기 전에 알림 삭제 또는 취소


예 내가 id,namePerson 객체를 가지고 매번 내가 데이터베이스에 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를 통해 예약 뭔가를 취소하려면

+2

"특정 날짜에 알림이 설정되었습니다."- 이는 알림의 기능이 아닙니다. 이 작업을 어떻게 수행하는지 보여주는 [mcve]를 제공 할 수 있습니다. – CommonsWare

+0

알람 관리자 사용 @CommonsWare – Andre

+0

@Andre 코드 예제를 작성하십시오. – mixel

답변

1

, 당신은 첫 번째 장소에서 작업을 예약하는 데 사용되는 하나에 해당하는 PendingIntent를 전달 AlarmManagercancel()를 호출합니다. 여기서, "등가 PendingIntent"로, I의 의미 :

  • 동일한 동작을 (예를 들어, getActivity()getService()getBroadcast())
  • (이러한 방법으로 2 파라미터) 동일한 요청 코드
  • 동등한 "Intent 해당"으로 Intent

, 내 말은 :

  • 같은 구성 요소
  • 동일한 작업
  • 같은 MIME 타입
  • 같은 범주 당신이 원래 설정 한 해당 속성의 어떤을 위해
  • 동일한 데이터 (Uri)

원래 PendingIntent의 경우 Intent

+0

지금 코드로 질문보기 :) – Andre

+0

하지만 당신의 대답을 이해합니다 :) 나는 잠에서 깨어나려고 노력할 테니 먼저 잠을 자야합니다. – Andre

+0

그것은 작동합니다. 감사 – Andre

관련 문제