2017-04-19 15 views
-2

1 일, 3 일, 7 일, 28 일 후에 서로 다른 시간대에 알람을 설정해야하는 프로젝트에서 작업 중입니다. 사용자가 앱을 종료했습니다. 나는 쉽게1 일, 3 일, 7 일 및 28 일과 같은 특정 요일에 대한 알림 설정 방법

 Calendar calendar2 = Calendar.getInstance(); 
     calendar2.set(Calendar.HOUR_OF_DAY, 16); 
     calendar2.set(Calendar.MINUTE, 40); 
     calendar2.set(Calendar.SECOND, 0); 
     alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar2.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); 
+0

질문 책, 도구, 소프트웨어 라이브러리, 자습서 또는 다른 오프 사이트 리소스를 권장하거나 찾기 위해 우리를 묻는 솔루션은 오프 주제 스택 오버플로 –

+1

https://developer.android.com/위한 것입니다 reference/android/app/AlarmManager.html – CommonsWare

답변

0

마침내이를 관리하여 일상에 대한 알람을 설정할 수 있어요. 아래

AlarmManager alarmManager = (AlarmManager) this.getSystemService(ALARM_SERVICE); 

    ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent(); 

    if (screenPreferences.getInt("summary_page", 0) == 0) { 
     if(screenPreferences.getInt("alarm_set", 0) == 0){ 
     screenPreferences.edit().putInt("alarm_set",1).commit(); 
     startNotifications(alarmManager, intentArray, 1); // 1 is to start notifications 
     } 
    }else { 
     screenPreferences.edit().putInt("alarm_set",0).commit(); 
     startNotifications(alarmManager, intentArray, 0); // 0 is to stop notifications 
    } 

/************************************************************/ 
private void startNotifications(AlarmManager alarmManager, ArrayList<PendingIntent> intentArray, int onOrOff) { 

    for (int i = 0; i < 5; ++i) { 
     Intent intent = new Intent(MainActivity.this, StartReceiver.class); 

     PendingIntent pendingIntent1 = PendingIntent.getBroadcast(MainActivity.this, i, intent, 0); 
     // Single alarms set for 1 day, 3 days, 7 days, 14 days, 28 days 
     if(onOrOff == 1){ 
     long daySet = 60 * 60 * 24 * 1000; 
     switch (i){ 
      case 0: daySet = 60 * 60 * 24 * 1000; // 1 day 
        break; 
      case 1: daySet = 60 * 60 * 72 * 1000; // 3 days 
        break; 
      case 2: daySet = 60 * 60 * 168 * 1000; // 7 days 
        break; 
      case 3: daySet = 60 * 60 * 336 * 1000; // 14 days 
        break; 
      case 4: daySet = 60 * 60 * 672 * 1000; // 28 days 
        break; 
      default: 
        daySet = 60 * 60 * 24 * 1000; // default 1 day 
        break; 
      } 

     alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + daySet, pendingIntent1); 
     intentArray.add(pendingIntent1); 
     } 
     else { 
      alarmManager.cancel(pendingIntent1); 
     } 
    } 
}