2017-04-18 2 views
0

안녕하세요. 내 코드으로 내 애플리케이션에서 alarm을 시작하십시오. 위의 코드 내 과소에서지정된 요일 이후에 알람 시작

public static void startReferAlarm(Context context,String[] message,String activityToOpen) 
{ 
    try { 

     Log.d("Refer friend Activity", "Alarm On"); 
     Calendar calendar = Calendar.getInstance(); 

     calendar.set(Calendar.HOUR_OF_DAY, 8); 
     Intent myIntent = new Intent(context, AlarmReciever.class); 
     myIntent.putExtra("message",message); 
     myIntent.putExtra("äctivityName",activityToOpen); 

     final int _id = (int) System.currentTimeMillis(); 
     PendingIntent appIntent = PendingIntent.getBroadcast(context, _id, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

     //48*60*60*1000 

     AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 

     alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
       48*60*60*1000, appIntent); 

    }catch (Exception e) { 
     Log.d("MyActivity", "Alarm Off"); 
     e.printStackTrace(); 
    } 
} 

알람을 시작하고 오전 8시에 통지합니다.을 입력 한 다음 48 시간 후에 반복합니다.

그러나 48 hours 다음에 알람을 시작하거나 7 hours 이후에 말하고 나서 48 hours을 반복합니다.

로직을 도와주세요. 미리 감사드립니다. 이 8'o 시계로 시간을 설정합니다으로

+0

시작 시간을 추가하는 방법을 묻는 질문이 있으십니까? 'Calendar # add()'메소드를 보라. –

+0

안녕 마이크. 아니. 초기 발사 요일과 시간을 설정하는 방법을 묻습니다. 예를 들어. 응용 프로그램을 설치했지만 프로파일을 작성하지 않았습니다. 따라서 응용 프로그램은 7 일 동안 기다린 다음 알람을 시작하여 프로필을 만들도록 요청합니다. 이 알람은 사전 정의 된 시간에 대체 요일에 반복됩니다. –

+0

그래, 그렇다. 초기 경보의 현재 시작 시간에 7 일을 추가하려고합니다. –

답변

1

는 24시간 형식으로 사용하려는 경우

//if you want to add 7 hours 
calender.add(Calender.HOUR, 7); //this will add 7 hours to current time 

//if you want to add 7 days 
calender.add(Calender.DAY_OF_MONTH, 7); //this will add 7 days to current time 

// interval to repeat alarm after 48hours 
int interval = 48 * 60 * 60 * 1000; 

Calendar.HOUR_OF_THE_DAYCalendar.HOUR를 교체하십시오 calender.set(Calendar.HOUR, 8)를 사용하지 마십시오.

여전히 혼동하는 모든 일에서 알람 문제 How to repeat alarm in android 6.0

+0

힌트를 위해 이봐. 앱을 7 일간 설치 한 후 알람을 실행하고 대체 요일에 알람을 다시 시작하는 것은 어떨까요? calender.add() 메소드에서 전달해야하는 것. –

+0

답변을 – Pehlaj

+1

Thnx로 업데이트했습니다. 이것은 내 코드입니다. calendar.add (DAY_OF_MONTH, 7); 7 일 후에 출시됩니다. calendar.set (Calendar.HOUR_OF_DAY, 9); 그날 아침 9시에 시작됩니다. 48 시간 간격으로 반복하십시오. alarmManager.setInexactRepeating (AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 48 * 60 * 60 * 1000, createProfileAppIntent); –

1

을 반복하는 답변을 참조하십시오. 아래 방법은 오전 7시에 알림을 보내고 48 시간 후에 반복되는 특정 조치가 발생한 후 7 일 후에 경보를 시작합니다.

public static void startCreateProfileAlarm(Context context,AlarmManager alarmManager ,String[] message,String activityToOpen) { 

    try { 

     //working code 
     Calendar calendar = Calendar.getInstance(); 
     calendar.add(DAY_OF_MONTH,7); 
     //calendar.add(Calendar.HOUR_OF_DAY,1); 
     calendar.set(Calendar.HOUR_OF_DAY, 9); 

     // we can set any time here 
     //calendar.set(Calendar.HOUR_OF_DAY, 10); 

     Intent myIntent = new Intent(context, AlarmReciever.class); 
     myIntent.putExtra("message",message); 
     myIntent.putExtra("äctivityName",activityToOpen); 


     final int _id = (int) System.currentTimeMillis(); 

     createProfileAppIntent = PendingIntent.getBroadcast(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); 


     //48*60*60*1000 
     //2*60*1000 
     alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 
       48*60*60*1000, createProfileAppIntent); 


    }catch (Exception e) { 
     Log.d("MyActivity", "Alarm Off"); 
     e.printStackTrace(); 
    } 
} 
관련 문제