2012-02-18 2 views
0

알람을 생성하여 2 분마다 애플리케이션을 시작하라는 알림을 사용자에게 보낼 수있게하려고합니다. 모든 것은 정상적으로 작동하지만 응용 프로그램을 수동으로 시작할 때 알림이 나타납니다. 코드가 실행되지 않을 때 난 단지 반지에 대한 통지를 원하는AlarmReceiver의 Android 알림

cal.set(Calendar.HOUR_OF_DAY, 12); 
    cal.set(Calendar.MINUTE, 01); 
    cal.set(Calendar.SECOND, 0); 

    Intent intent = new Intent(myactivity.this, AlarmReceiver.class); 

    PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 
      PendingIntent.FLAG_CANCEL_CURRENT); 

    // Get the AlarmManager service 
    am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 2 * 60 * 1000, sender); 

:

수신기 :

public void onReceive(Context context, Intent intent) { 
    nm = (NotificationManager) context 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    CharSequence from = "myactivity"; 
    CharSequence message = "click to start activity"; 


    Intent scheduledIntent = new Intent(context, AmslerTestActivity.class); 


    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
      scheduledIntent, 0); 
    Notification notif = new Notification(R.drawable.icon, 
      message, System.currentTimeMillis()); 
    notif.setLatestEventInfo(context, from, message, contentIntent); 
    notif.flags = Notification.FLAG_AUTO_CANCEL; 
    notif.defaults |= Notification.DEFAULT_SOUND; 
    nm.notify(1, notif); 
    scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(scheduledIntent); 

} 

스케줄러 여기 내 코드입니다.

답변

0

, onStop 또는 onDestroy에 코드의 "스케줄러"부분을 넣어서 Activity이 중지 된 후에 만 ​​알람을 설정하십시오. 귀하의 목적에 가장 적합한 것을 결정하기 위해 here을 읽으십시오. 그런 다음 am.setRepeating을 수행 할 때 triggerAtTimeSystem.currentTimeMillis() + 2 * 60 * 1000을 사용하면 Activity이 정지 한 때로부터 2 분간 경보가 울립니다. 그런 다음 2 분마다 올바르게 설정 한 세 번째 매개 변수 인 interval을 기반으로 2 분마다 반복됩니다.

+0

답장 보내 주셔서 감사합니다. 그러나 그것은 효과가 없었습니다. 나는 "스케줄러"를 onDestroy에 두었고 알림은 다시 클릭하면 나옵니다. 2 분은 "테스트"일입니다. 실제 값은 INTERVAL_DAY가됩니다. 매일 12시 1 분에 통보를하고 싶습니다. 방송 수신자가 즉시 수신되는 것과 같습니다. 내가 잘못한 일이 있습니까? – noviceAndroidprogrammer

+0

그래서 지금 경보를 설정하고있는 것입니까? : 'am.setRepeating (AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (2 * 60 * 1000), 2 * 60 * 1000, sender); 과거에 트리거 시간으로 설정 한 알람은 즉시 알람을 울리게합니다. 또한, 당신은 여전히 ​​당신이 설정 한 이전 경보가 있습니다. 취소하거나 장치/에뮬레이터의 전원을 끄고 다시 켜서 알람을 지울 수 있습니다. – koopaking3

+0

또한 "내가 클릭 한 순간"을 의미합니까? 'onDestroy'에 알람을 설정하면'Activity'에서'finish()'를 호출하거나 시스템이 공간을 절약하기 위해 활동을 파괴 할 때만 해당 코드를 호출합니다. 따라서 뒤로 버튼을 누르 자마자 알람이 울리는 것 같지 않습니다. 뭔가 다른 일이 계속되어야합니다. – koopaking3