2016-12-10 2 views
0

경고 관리자가 작동하기 전에. 나는 아무것도 바꾸지 않았다고 생각하지만 지금은 전혀 시작하지 않습니다.AlarmManager가 서비스를 시작하지 않음

SettingsActivity.java 

Intent intent; 
static PendingIntent recurringDownload; 

intent = new Intent(context, UpdateScoresService.class); 

recurringDownload = PendingIntent.getService(context, 0, intent, 0); 
Preference.OnPreferenceChangeListener refreshListener = new Preference.OnPreferenceChangeListener() { 
       @Override 
       public boolean onPreferenceChange(Preference preference, Object newValue) { 
        if(newValue.toString().equals("1")){ /* daily */ 
         background_refresh.setSummary("Scores will be refreshed daily."); 
         AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE); 
         manager.cancel(recurringDownload); 
         recurringDownload.cancel(); 
         Log.e("DAILY REFRESH", " "); 
         Calendar calendar = Calendar.getInstance(); 
         calendar.setTimeInMillis(System.currentTimeMillis()); 
         calendar.set(Calendar.HOUR_OF_DAY,10); 
         calendar.set(Calendar.MINUTE,00); 
         if(calendar.before(Calendar.getInstance())){ 
          Log.e("AFTER", "10 AM DAILY"); 
          calendar.add(Calendar.DATE, 1); 
         } 
         manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, recurringDownload); 
        }else if(newValue.toString().equals("2")){ /* weekly */ 
         Log.e("WEEKLY REFRESH", " "); 
         background_refresh.setSummary("Scores will be refreshed weekly."); 
         AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE); 
         manager.cancel(recurringDownload); 
         recurringDownload.cancel(); 
         Calendar calendar = Calendar.getInstance(); 
         calendar.setTimeInMillis(System.currentTimeMillis()); 
         calendar.set(Calendar.HOUR_OF_DAY,10); 
         calendar.set(Calendar.MINUTE,00); 
         if(calendar.before(Calendar.getInstance())){ 
          Log.e("AFTER", "10 AM WEEKLY"); 
          calendar.add(Calendar.DATE, 1); 
         } 
         manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, recurringDownload); 
        }else{ /* manually */ 
         background_refresh.setSummary("Scores will be refreshed manually."); 
         Log.e("MANUAL REFRESH", " "); 
         AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE); 
         manager.cancel(recurringDownload); 
         recurringDownload.cancel(); 

        } 
        return true; 
       } 
}; 

UpdateScoresService가 여기에 있습니다 : : 여기

내가 알람 매니저를 설정하는 코드입니다

public class UpdateScoresService extends IntentService { 

    public int countChanged; 
    Context context = this; 

    public UpdateScoresService() { 
     super("UpdateScoresService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     Log.e("onHandleIntent", "grabbing scores"); 
     countChanged = new GetAnimeScores(getApplicationContext()).refreshScores(); 

     if(countChanged>0){ //Display notification if any scores changed 
      Log.d("Creating notification", " "); 
      NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
      builder.setSmallIcon(R.drawable.ic_timeline_white_24dp); 
      builder.setContentTitle("MAL Score Tracker"); 
      builder.setAutoCancel(true); 

      if(countChanged==1){ 
       builder.setContentText("1 score changed since you were gone!"); 
      }else{ 
       builder.setContentText(countChanged+" scores changed since you were gone!"); 
      } 

      Intent intent1 = new Intent(context, MainActivity.class); 
      TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
      stackBuilder.addParentStack(MainActivity.class); 
      stackBuilder.addNextIntent(intent1); 
      PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT); 
      builder.setContentIntent(pendingIntent); 

      NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
      notificationManager.notify(0,builder.build()); 
     } 
    } 
} 
} 

서비스 않습니다에서 onHandleIntent에서 SettingsActivity 프린트에 로그인하지만 로그 인쇄되지 않습니다. 나는 무엇이 잘못되었는지 확신하지 못한다.

+0

BroadcastReciever 또는 WakefulBr을 시작하려면 경보가 필요할 수 있습니다 oadcastReciever 그러면 서비스가 시작됩니다. –

+0

@NickFriskel 흠 무슨 뜻입니까? 코드 예제를 볼 수 있습니까? 감사. – user2923535

답변

0

서비스 시작에 대한 책임은 BroadcastReceiver입니다. 그것의 코드는 다음과 같이 보일 것이다 :

<receiver android:name=".ReceiverToStartService"/> 

지금 당신이 전달하는 당신의 활동에 텐트를 변경 : 매니페스트에

public class ReceiverToStartService extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     Intent i = new Intent(context, UpdateScoresService.class); 
     ComponentName service = context.startService(i); 
    } 
} 

등록 수신기 :

는 브로드 캐스트 리시버 클래스 만들기 ~ PendingIntent :

intent = new Intent(context, ReceiverToStartService.class); 
recurringDownload = PendingIntent.getService(context, 0, intent, 0); 
+0

Nope은 여전히 ​​실행되지 않았습니다. 이것은 매우 이상합니다. 그것은 이전에 일하고 있었고 몇 달 전에 작성한 이래로 손도 대지 않았습니다. 그런 다음 일주일 전에 RetroFit으로 전환했을 때 네트워크 호출로 인해 작업이 중단 된 것으로 보입니다. 도움이된다면 getService를 getBroadcast로 변경했지만 여전히 주사위처럼 보이지 않습니다. – user2923535

+0

매니페스트에 서비스를 등록하셨습니까? – Marat

+0

예 등록되었습니다. – user2923535

관련 문제