title: How can I make sure that the service still works if the app is closed?
body: <p>I have an app that I want to open an activity every three minutes, and when the app is open or in recent app, this was done correctly, but when the app exits from recent app, it no longer works.
  Even I have set an alarm to check every ten minutes that if the service is closed it will run again but it does not seem to work.</p>
  <p>this is my service code:</p>
  <p>public class SampleService extends JobService {</p>
  <pre><code>Alarm alarm = new Alarm();
  private Timer timer;
      @Override
      public boolean onStartJob(JobParameters params ) {
          alarm.setAlarm(this);
          start();
          return true;
      }
      @Override
      public boolean onStopJob( JobParameters params ) {
          Toast.makeText(this, &quot;Service stop!&quot;, Toast.LENGTH_LONG).show();
          return false;
      }
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      return super.onStartCommand(intent, flags, startId);
  }
  private TimerTask timerTask = new TimerTask() {
      @Override
      public void run() {
          Intent intent = new Intent(SampleService.this, SplashScreen.class);
          intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          startActivity(intent);
      }
  };
  public void start() {
      if(timer != null) {
          return;
      }
      timer = new Timer();
      timer.scheduleAtFixedRate(timerTask, 180000, 180000);
  }
  public void stop() {
      timer.cancel();
      timer = null;
  }
  }
  </code></pre>
  <p>and this is my Alarm code:</p>
  <p>public class Alarm extends BroadcastReceiver{</p>
  <pre><code>private JobScheduler jobScheduler;
  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  @Override
  public void onReceive(Context context, Intent intent) {
  
      PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
      @SuppressLint(&quot;InvalidWakeLockTag&quot;) PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK , &quot;&quot;);
      wl.acquire();
  
      if (!isMyServiceRunning(SampleService.class , context)){
          jobScheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
  
          ComponentName serviceName = new ComponentName(context.getPackageName(),
                  SampleService.class.getName());
  
          JobInfo.Builder jobBuilder = new JobInfo.Builder(10, serviceName);
  
          JobInfo myJobInfo = jobBuilder.build();
          jobScheduler.schedule(myJobInfo);
  
          Toast.makeText(context, &quot;12345&quot;, Toast.LENGTH_LONG).show();
      }
      Toast.makeText(context, &quot;Alarm !!!!!!!!!!&quot;, Toast.LENGTH_LONG).show(); // For example
  
      wl.release();
  }
  public void setAlarm(Context context)
  {
      AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
      Intent i = new Intent(context, Alarm.class);
      PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
      am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi); // Millisec * Second * Minute
  }
  
  public void cancelAlarm(Context context)
  {
      Intent intent = new Intent(context, Alarm.class);
      PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
      AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
      alarmManager.cancel(sender);
  }
  
  private boolean isMyServiceRunning(Class&lt;?&gt; serviceClass , Context context) {
      ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
      for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
          if (serviceClass.getName().equals(service.service.getClassName())) {
              return true;
          }
      }
      return false;
  }
  }
  </code></pre>
  <p>Does anyone know what I should do to fix this?</p>