마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 alarmmanager질문

0 추천
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      // 통지 매니저를 취득
      setmNotification((NotificationManager) getSystemService(NOTIFICATION_SERVICE));

      // 알람 매니저를 취득
      mManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
      // 현재 시각을 취득
      mCalendar = new GregorianCalendar();
      Log.i("HelloAlarmActivity", mCalendar.getTime().toString());
      // 셋 버튼, 리셋버튼의 리스너를 등록
      setContentView(R.layout.activity_main);
      Button b = (Button) findViewById(R.id.set);
      b.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
            Toast.makeText(getApplicationContext(), " 설정완료  >.< ", Toast.LENGTH_SHORT).show();
            setAlarm();
         }
      });

      b = (Button) findViewById(R.id.reset);
      b.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
            resetAlarm();
         }
      });
      start_music = MediaPlayer.create(getBaseContext(), R.raw.abc);

      // 일시 설정 클래스로 현재 시각을 설정
      mDate = (DatePicker) findViewById(R.id.date_picker);
      mDate.init(mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH),
            mCalendar.get(Calendar.DAY_OF_MONTH), this);
      mTime = (TimePicker) findViewById(R.id.time_picker);
      mTime.setCurrentHour(mCalendar.get(Calendar.HOUR_OF_DAY));
      mTime.setCurrentMinute(mCalendar.get(Calendar.MINUTE));
      mTime.setOnTimeChangedListener(this);
   }

   // 알람의 설정
   private void setAlarm() {
      mManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, mCalendar.getTimeInMillis(),
            pendingIntent());
        Log.i("HelloAlarmActivity", mCalendar.getTime().toString());
   }

   // 알람의 해제
   private void resetAlarm() {
      mManager.cancel(pendingIntent());
      start_music.stop();
   }

   // 알람의 설정 시각에 발생하는 인텐트 작성
   private PendingIntent pendingIntent() {
      Toast.makeText(getApplicationContext(), " 알람시간  >.< ", Toast.LENGTH_SHORT).show();
      start_music.start();
      return null;
   }
   
      
   // 일자 설정 클래스의 상태변화 리스너
   public void onDateChanged(DatePicker view, int year, int monthOfYear,
         int dayOfMonth) {
      mCalendar.set(year, monthOfYear, dayOfMonth, mTime.getCurrentHour(),
            mTime.getCurrentMinute());
      Log.i("HelloAlarmActivity", mCalendar.getTime().toString());
      
   }

   // 시각 설정 클래스의 상태변화 리스너
   public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
      mCalendar.set(mDate.getYear(), mDate.getMonth(), mDate.getDayOfMonth(),
            hourOfDay, minute);
      Log.i("HelloAlarmActivity", mCalendar.getTime().toString());
   }

   public NotificationManager getmNotification() {
      return mNotification;
   }

   public void setmNotification(NotificationManager mNotification) {
      this.mNotification = mNotification;
   }
}

        set버튼을 눌러 시간을 설정하게 되면 설정된시간에 음악이나오는게 아니라 set버튼을 눌르자마자 노래가나옵니다. 어떤 문제점이 있는건가여 ???

익명사용자 님이 2015년 7월 14일 질문

1개의 답변

0 추천
알람 매니저는 과거의 시간으로 세팅하면 즉시 동작됩니다.
쎄미 (162,410 포인트) 님이 2015년 7월 14일 답변
그게 무슨말이져...저희가 과거의시간으로 세팅햇다는건가여??초짜라 자세한 답변좀 부탁드릴게여 ㅜㅜ 부탁드립니다!!!
mManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, mCalendar.getTimeInMillis(), pendingIntent());
여기에 쓰인 mCalendar는 현재 시간이니까 세팅하자마자 과거가 되고, 바로 실행이 되는거죠.
그럼 어떤 방식으로 변경을해야델까여 ???답답해도 자세하게알려주세여 죄송합니다........
5분 뒤로 설정해보세요.
...