2016-10-27 3 views
-2

오늘의 특정 시간에이 서비스를 시작하려고하지만 응답하지 않습니다. 그 이유에 대한 이유가 있습니까?알람 관리자가 특정 시간에 실행됩니다.

String time = sharedPrefs.getString("change_time", ""); 
    String[] parts = time.split(":"); 
    int chosenHour = Integer.parseInt(parts[0]); 
    int chosenMinute = Integer.parseInt(parts[1]); 
    Calendar cal = Calendar.getInstance(); 
    cal.set(Calendar.HOUR_OF_DAY, chosenHour); 
    cal.set(Calendar.MINUTE, chosenMinute); 
    cal.set(Calendar.SECOND, 0); 
    cal.set(Calendar.MILLISECOND, 0); 

    Intent i = new Intent(this, SmsService.class); 
    PendingIntent pIntent = PendingIntent.getService(this, 0, i, 0); 
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 86400000, pIntent); 
    return super.onStartCommand(intent, flags, startId); 

답변

0

코드를 어디에 넣으시겠습니까? 내가 볼로 그래, 당신이 당신의 서비스를 시작 않은 경우?

을 예약 AlarmManager를 들어 (A Service 내부 super.onStartCommand(intent, flags, startId)가?, 나는 Application#onCreate()에 코드를 삽입하는 것이 좋습니다 거라고입니다.

+0

Service.StartService을()? 그리고 어디에 넣어야합니까? –

+0

예, API입니다. "어디에서 그것을 넣을 까?" => 필요에 따라 예 : 서비스를 즉시 시작하려면'Application # onCreate()'에 넣거나, 어떤 이벤트/액션에 기반하여 트리거하고 싶다면, 이벤트/액션이 발생할 때 메소드를 호출하십시오. 그러나 이전에 말했듯이, 코드를 애플리케이션 내부가 아닌'Application # onCreate()'에 넣어야합니다. –

0

전화에서이 metod 당신의 MainActivity :

private void setNotifyAlarm() { 
     long _alarm; 
     Calendar now = Calendar.getInstance(); 
     Calendar wakeupcall = Calendar.getInstance(); 
     wakeupcall.setTimeInMillis(System.currentTimeMillis()); 
     wakeupcall.set(Calendar.HOUR_OF_DAY, 15); 
     wakeupcall.set(Calendar.MINUTE, 30); 

     if (wakeupcall.getTimeInMillis() <= now.getTimeInMillis()) 
      _alarm=wakeupcall.getTimeInMillis() + (AlarmManager.INTERVAL_DAY+1); 
     else 
      _alarm=wakeupcall.getTimeInMillis(); 


     al = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
     notif= new Intent(this,NotifyService.class); 
     fintent = PendingIntent.getService(this,0,notif,0); 
     al.setRepeating(AlarmManager.RTC_WAKEUP,_alarm,AlarmManager.INTERVAL_DAY, fintent); 

    } 

이 메서드 호출 서비스 15:30 매일 이제 서비스 클래스에서 :.

@Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     String time = sharedPrefs.getString("change_time", ""); 
    String[] parts = time.split(":"); 
    int chosenHour = Integer.parseInt(parts[0]); 
    int chosenMinute = Integer.parseInt(parts[1]); 
    Calendar cal = Calendar.getInstance(); 
    cal.set(Calendar.HOUR_OF_DAY, chosenHour); 
    cal.set(Calendar.MINUTE, chosenMinute); 
    cal.set(Calendar.SECOND, 0); 
    cal.set(Calendar.MILLISECOND, 0); 

    Intent i = new Intent(this, SmsService.class); 
    PendingIntent pIntent = PendingIntent.getService(this, 0, i, 0); 
    AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 86400000, pIntent); 
     return START_STICKY; 
    } 
01 23,516,

이 당신의 매니페스트 파일에 ADD :

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/> 
<service android:name=".NotifyService" android:exported="true" android:enabled="true"/> 
관련 문제