2016-12-10 5 views
0

以前は、アラームマネージャが動作していました。私は何かを変えたとは思わないが、今は全く始まっていない。ここでAlarmManager not Starting Service

は、私はアラームマネージャを設定するコードです:

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()); 
     } 
    } 
} 
} 

SettingsActivity印刷でログインけどonHandleIntentでログサービスからか印刷されません。私は何が間違っているのか分かりません。

+0

BroadcastRecieverまたはWakefulBrを起動するにはアラームが必要な場合がありますoadcastRecieverサービスを開始します。 –

+0

@NickFriskel Hmmそれはどういう意味ですか?コード例が見えますか?ありがとう。 – 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); 
    } 
} 

登録レシーバー:

はBroadcastReceiverクラスを作成します。 〜PendingIntent:

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

Nopeはまだトリガーしていないようです。これは非常に奇妙です。数か月前に書きましたが、私は1週間前にRetroFitに切り替わったときに、これが動作を停止したように見えました。私はgetServiceをgetBroadcastに変更しましたが、それが助けてもまだサイコロのようには見えません。 – user2923535

+0

サービスをマニフェストに登録しましたか? – Marat

+0

はい登録しました。 – user2923535