2013-07-18 3 views
0

죄송합니다 이전 대답하지만 난 모든 곳에서 보았다하지만 난 자동 오전 9 일상에서 알림을 화재 알람 관리기를 사용하고잘못된 시간에 응답 알람 관리기

적절한 솔루션을하지 않았지만, 만약 내가 그것을 실행하려고하면 에뮬레이터는 즉시 실행되며 매 30 분마다 (정확히는 31-32 분) 실행됩니다.

왜 그런가? 도움을 주셨습니다.

코드는 다음과 같습니다 :

public class Home extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_bsheet); 

    notificationAlert(savedInstanceState); 

} 

    private void notificationAlert(Bundle savedInstanceState) { 

    AlarmManager manager; 
    PendingIntent pendingIntent; 
    Intent intent=new Intent(Home.this, Notify.class); 
    manager=(AlarmManager)getSystemService(ALARM_SERVICE); 
    pendingIntent=PendingIntent.getService(Home.this, 
      0, intent, 0); 
    GregorianCalendar gcal = new GregorianCalendar(); 
    gcal.set(Calendar.HOUR_OF_DAY, 9); 
    gcal.set(Calendar.MINUTE, 0); 
    gcal.set(Calendar.SECOND, 0); 
    gcal.set(Calendar.MILLISECOND, 0); 

    long initTime = gcal.getTimeInMillis(); 

    manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime, 
      24*60*60*1000, pendingIntent); 
} 

} 

환호,

편집 : 내 의도 앱이 설치되면, 그것은 오전 9에서이 경보를 발생한다는 것입니다. 나는 알람을 onCreate에 넣었습니다. 알람이 앱을 시작할 때마다 생성되고, 앱을 숨길 때 이상한 일이 일어나는지 확실하지 않습니다. 다시 통찰력을 얻었습니다!

답변

0

이 시도 :

Calendar currentTime = Calendar.getInstance(); 
    Calendar alarmTime = Calendar.getInstance(); 
    currentTime.set(Calendar.HOUR_OF_DAY, 9; 
    currentTime.set(Calendar.MINUTE, 0); 
    currentTime.set(Calendar.SECOND, 0); 
    currentTime.set(Calendar.MILLISECOND, 0); 
    if (alarmTime.compareTo(currentTime) <= 0) { 
// check for time 
alarmTime.add(Calendar.DATE, 1); 
} 

Intent intent = new Intent(getBaseContext(), Receiver.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(
    getBaseContext(), 1, intent, 0); 
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(), 
         AlarmManager.INTERVAL_DAY, pendingIntent); 
+0

감사합니다. 안타깝게도, 앱을 시작할 때마다로드됩니다 ... 나는 몇 가지 수정안을 작성하고 곧바로 완전한 해결책을 보여줄 것입니다. –

+0

다음에 시간을 비교하고 알람을 시작하십시오. –

+0

편집을 참조하십시오 ... 비교 비트를 수행하는 방법을 모르십니까? –

0
manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime, 24*60*60*1000, pendingIntent); 

알람 매니저가 immidiatly를 경우 initTime < System.currentTimeMillis()

from docs 실행됩니다 : 시간이 과거에 발생

경우, 경보가 즉시 발생됩니다 과거의 트리거 시간이 th에 비례하는 정도에 따라 알람이 발생합니다. 반복 간격.

당신이 제공 한 코드에 따르면, gcal.getTimeInMillis() 오늘 9.00에 해당하는 millisecods를 반환합니다. 따라서이 시간이 지나면 코드를 실행하려고 시도하면 initTime이 AlarmManager의 실행을 트리거하는 현재 시스템 시간보다 짧아집니다.

는이 문제를 해결하려면, 예를 들어, 전달하기 전에 일정을 하루 추가 할 수 있습니다 자신이 아침에게이 내일을 실행

갱신 1을 수 있도록 에 내일 9.00을 가리 킵니다 그래서 과거에 이미있는 경우 gcal.getTimeInMillis()

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    initManager(); 
} 
private void initManager() { 
    AlarmManager manager; 
    PendingIntent pendingIntent; 
    Intent intent = new Intent(this, NotifyService.class); 
    manager=(AlarmManager)getSystemService(ALARM_SERVICE); 
    pendingIntent=PendingIntent.getService(this, 0, intent, 0); 

    long initTime = System.currentTimeMillis() + 5000; 

    manager.setRepeating(AlarmManager.RTC_WAKEUP, initTime, 
      10*1000, pendingIntent);   
} 

: -

시도 코드 같은

나를 위해 예상대로 일 서비스 10 초마다 해고 그러나 다른 옵션을 사용할 수 있습니다 : 당신은 그래서 runTime에서 서비스를 발사합니다이

long runTime = /* your calendar time + 24 hours in millis*/ 
manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent);  

처럼 해고 할 수 AlarmManager.set() 방법이 있습니다.

public MainActivity extends Activity{ 

    protected onCreate(Bundle bundle){ 
     .... 
     initManager(); 
    } 

    private initManager(){ 
     ... 
     long runTime = /* time of your first alarm/notification/whatever you develope */ 

     Intent intent = new Intent(this, NotifyService.class); 
     pendingIntent=PendingIntent.getService(this, 0, intent, 0); 
     manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent); 
    } 
} 

그리고 서비스 :이처럼 뭔가 볼 수 있도록 다음 서비스에 다른 날을 위해 다시 계산 runTime과 같은 방법을 호출

public NotifyService extends Service{ 
    ... 
    public int onStartCommand(...){ 
     ... 
     /* do stuff */ 

     Intent intent = new Intent(getApplicationContext(), NotifyService.class); 
     pendingIntent=PendingIntent.getService(this, 0, intent, 0); 
     long runTime = /* recalculate it according to the next time of firing this service*/ 
     manager.set(AlarmManager.RTC_WAKEUP, runTime, pendingIntent); 

    } 

을}

그래서 서비스 것 서비스가 시작될 때마다 AlarmManager 자체에 인 텐트를 등록하십시오.

+0

감사합니다. onCreate의 일부로 AlarmManager에 의해 잘못된 위치에 배치 했습니까? –

+0

흥미 롭습니다. 당신의 경우에 무엇이 잘못 될 수 있는지 아직도 모른다. OnCreate가 문제가되어서는 안됩니다. 그러나 그것은 10 초마다 발사를위한 내 테스트 케이스에서 잘 작동합니다. 당신이 아직도 그것에 붙어 있다면 나는 당신이 원하는 것을 얻기 위해 또 다른 속임수를 제안 할 수 있습니다. 위의 update1을 확인하십시오. –

관련 문제