title: Chinese Android Devices Killing Background Service
body: <p>I am trying to run a background service in an android application. I have tried the standard way of doing it and a lot of other tweaks. The problem is that when the application is closed from recent apps, the service gets killed. This only happens in Chinese Android devices like Oppo, Vivo, Xiomi, etc. It works fine on all other brands.</p>
  
  <p>I have tried the following solutions.</p>
  
  <ol>
  <li><p>Over-riding the <code>OnStartCommand()</code> of activity to return <code>START_STICKY</code>. This still not re-starts activity after the application is closed as expected.</p>
  
  <pre><code>@Override
      public int onStartCommand(Intent intent, int flags, int startId) {
  
          return START_STICKY;
      }
  </code></pre></li>
  <li><p>Added the application to exceptions of battery saving, so that it doesn't close it to save battery in <code>DOZE</code> mode.</p></li>
  <li><p>Gave the application permission to <code>Auto-Start</code> from the security settings of phone.</p></li>
  <li><p>Started the service as a foreground service, including a persistent notification.</p>
  
  <pre><code>@Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      String input = intent.getStringExtra("inputExtra");
      createNotificationChannel();
      Intent notificationIntent = new Intent(this, MainActivity.class);
      PendingIntent pendingIntent = PendingIntent.getActivity(this,
              0, notificationIntent, 0);
      Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
              .setContentTitle("Foreground Service")
              .setContentText(input)
              .setSmallIcon(R.drawable.ic_launcher_foreground)
              .setContentIntent(pendingIntent)
              .build();
      startForeground(1, notification);
      //do heavy work on a background thread
      //stopSelf();
      return START_NOT_STICKY;
  }
  </code></pre></li>
  <li><p>Implemented Alarm Manager approach of starting service after it gets killed in <code>onTaskRemoved()</code> and <code>onDestroy()</code> methods of service.</p>
  
  <pre><code>&lt;service
       android:name=".MyService"
       android:enabled="true"
       android:exported="true"
       android:stopWithTask="false" /&gt;
  </code></pre>
  
  <pre><code>@Override
      public void onTaskRemoved(Intent rootIntent){
          Log.d("Service:","I am being closed!");
          Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
          restartServiceIntent.setPackage(getPackageName());
  
          PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
          AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
          alarmService.set(
                  AlarmManager.ELAPSED_REALTIME,
                  SystemClock.elapsedRealtime() + 1000,
                  restartServicePendingIntent);
          super.onTaskRemoved(rootIntent);
      }
  </code></pre></li>
  <li><p>I have also tried using a broadcast manager to start my service receiving a broadcast from activity when it is closed.</p>
  
  <pre><code>&lt;receiver
       android:name=".SensorRestarterBroadcastReceiver"
       android:enabled="true"
       android:exported="true"
       android:label="RestartServiceWhenStopped"
       /&gt;
  </code></pre>
  
  <pre><code>public class SensorRestarterBroadcastReceiver extends BroadcastReceiver {
  
      @Override
      public void onReceive(Context context, Intent intent) {
          Log.i(SensorRestarterBroadcastReceiver.class.getSimpleName(), "Service Stops!");
          context.startService(new Intent(context, MyService.class));;
      }
  }
  </code></pre></li>
  </ol>
  
  <p>However, the service is not started again in these chinese devices specifically. While background services of Whatsapp, Facebook and some famous apps return after a few minutes of closing the app. Please suggest the correct way to achieve this.</p>
  
  <p>I have tried solutions provided at <a href="https://stackoverflow.com/questions/48515502/background-service-is-not-restarting-after-killed-in-oppo-vivo-mi-android-vers/50291806">Background Service is not restarting after killed in oppo, vivo, mi android version 7.1.2</a> as also described above. It doesn't solve my problem.</p>