2011-12-07 5 views
1

AlarmManagerNotificationManagerBroadcastReceiver입니다. 특정 날짜의 알람을 설정하면 처음으로 작동합니다. 그러나 날짜를 수정하고 확인 버튼을 클릭하면 알람이 즉시 작동합니다 ( ). 만료일 이후의 알람 간격 요일을 고정 된 시간으로 설정하고 싶습니다. 문제가 무엇입니까? 나는 그 순간 이해하지 못한다.날짜가 바뀌면 알림이있는 Android 알람이

confirmButton.setOnClickListener(new View.OnClickListener() 
{ 
    public void onClick(View v) { 
    //set alarm with expiration date     
    am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    setOneTimeAlarm(); 
    Toast.makeText(fridgeDetails.this, "Alarm automatic set", 
     Toast.LENGTH_SHORT).show(); 
    setResult(RESULT_OK); 
    finish(); 
} 

public void setOneTimeAlarm() { 
    c.set(Calendar.HOUR_OF_DAY, 14); 
    c.set(Calendar.MINUTE, 49); 
    c.set(expiredYear, expiredMonth, expiredDay); 
    Intent myIntent = new Intent(fridgeDetails.this, AlarmService.class); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(
     fridgeDetails.this, 0, myIntent, PendingIntent.FLAG_ONE_SHOT); 
    am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 
     AlarmManager.INTERVAL_DAY, pendingIntent); 
} 
}); 

는 AlarmService.java

public class AlarmService extends BroadcastReceiver{ 
    NotificationManager nm; 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     nm = (NotificationManager) context.getSystemService(
      Context.NOTIFICATION_SERVICE); 
     CharSequence from = "Check your fridge"; 
     CharSequence message = "It's time to eat!"; 
     PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
      new Intent(), 0); 
     Notification notif = new Notification(R.drawable.ic_launcher, 
      "Keep Fridge", System.currentTimeMillis()); 
     notif.setLatestEventInfo(context, from, message, contentIntent); 
     notif.defaults |= Notification.DEFAULT_SOUND; 
     notif.flags |= Notification.FLAG_AUTO_CANCEL; 
     nm.notify(1, notif); 
    } 
} 
+0

"homeactivity"와 같은 집 활동 및 활동 이름을 실행한다고 가정처럼 의도에 Acitivity 이름을 전달 문제는 완전히 있지만, FLAG_ONE_SHOT을 사용하는 것은이 경우에는 차선책입니다. AlarmManager에서 하나의 콜백 만 허용하기 때문입니다. 대신 0을 사용해보십시오. –

답변

2

는 대신 FLAG_ONE_SHOT의 속성을 설정해야합니다. 이것은 반복이 아닌 단일 알람 이벤트입니다. 이

PendingIntent pendingIntent = PendingIntent.getBroadcast(
     fridgeDetails.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

에 대한 자세한 내용을 참조하십시오에서 here

편집 :

당신이 PendingIntent으로 통지를 할 것처럼이에서

PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(), 0); 

당신은 의도의 빈 개체를 전달 어떤 액티비티가 런치처럼 나타날지 알림을 클릭하면 클래스 이름을 전달해야합니다. 알람 설정 시간에 당신이 할

Intent myIntent = new Intent(fridgeDetails.this, AlarmService.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(
     fridgeDetails.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

지금 당신이이 해결되지 않는

PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(context,HomeActivity.class), 0); 
+0

고마워요! 알림을 클릭하면 존재하는 응용 프로그램이 열리지 않습니다. 알림 아이콘을 클릭하면 어떻게 열 수 있습니까? – wholee1

+0

편집 대답 게시판을 확인하십시오 – Pratik

+0

그 후, 어떻게 contentIntent를 호출 할 수 있습니까? – wholee1