0

I try to fire service with alarmmanager after an hour, but it does not work.

I tried alarmmanager.setAlarmClock and setExactAndAllowWhileIdle too, but it does not word on Android 8.0.

IN MAINACTIVITY

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

intent.setAction(PlayerService.ACTION_START_FOREGROUND_SERVICE);

    pendingIntent1 = PendingIntent.getService(getApplicationContext(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    pendingIntent2 = PendingIntent.getService(getApplicationContext(), 2, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    pendingIntent3 = PendingIntent.getService(getApplicationContext(), 3, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    pendingIntent4 = PendingIntent.getService(getApplicationContext(), 4, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    pendingIntent5 = PendingIntent.getService(getApplicationContext(), 5, intent, PendingIntent.FLAG_UPDATE_CURRENT);

long p1, p2, p3, p4, p5;

p1=System.currentTimeMillis()+ 60 * 60 * 1000L;
p2=System.currentTimeMillis()+ 105 * 60 * 1000L;
p3=System.currentTimeMillis()+ 240 * 60 * 1000L;
p4=System.currentTimeMillis()+ 310 * 60 * 1000L;
p5=System.currentTimeMillis()+ 400 * 60 * 1000L;

alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, p1, pendingIntent1);
    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, p2, pendingIntent2);
    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, p3, pendingIntent3);
    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, p4, pendingIntent4);
    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, p5, pendingIntent5);

MANIFEST: - service android:name=".PlayerService"


PLAYERSERVICE

public class PlayerService extends Service  {
    MediaPlayer mp;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mp = new MediaPlayer();
    }

     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {

    if(intent != null)
    {
        String action = intent.getAction();

        switch (action)
        {
            case ACTION_START_FOREGROUND_SERVICE:
                startThisForegroundService(intent);
                Toast.makeText(getApplicationContext(), "Foreground service is started.", Toast.LENGTH_LONG).show();
                break;
            case ACTION_STOP_FOREGROUND_SERVICE:
                stopThisForegroundService();
                Toast.makeText(getApplicationContext(), "Foreground service is stopped.", Toast.LENGTH_LONG).show();
                break;
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

   private void startThisForegroundService(Intent intent){
      mp-->setDatasource and prepare and start! 
   }

    private void stopThisForegroundService()
    {
        Log.d(TAG_FOREGROUND_SERVICE, "Stop foreground service.");
        stopForeground(true);
        stopSelf();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mp.stop();
        mp.release();
        Toast.makeText(this, "Service stopped...", Toast.LENGTH_SHORT).show();
    }
}

After 5 Minutes - it works perfekt. After 1 Hour - happens nothing After 5 Hours - same, happens nothing

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • 1
    Possible duplicate of [Alarmmanager does not fire Service after more than 1 Hour - or when the app is in doze... - Android Oreo](https://stackoverflow.com/questions/55529514/alarmmanager-does-not-fire-service-after-more-than-1-hour-or-when-the-app-is-i) – A Jar of Clay Apr 05 '19 at 08:41
  • Possible duplicate of [Android Oreo killing background services and clears pending alarms, scheduled jobs after entering doze mode](https://stackoverflow.com/questions/50504296/android-oreo-killing-background-services-and-clears-pending-alarms-scheduled-jo) – Nicolás Carrasco-Stevenson Apr 05 '19 at 08:43

0 Answers0