1

I have a service that should run in background.

I have applied solution from given link

START_STICKY does not work on Android KitKat

I am able to restart the service on all phones having android os jelly bean or kitkat.

But on Redmi (android os version 4.3) phone my service is still getting killed when I remove app from task manager and not restart again. How can I restart my service on Redmi phone.

I tried to restart service using AlarmManager.

private void StartLocationServiceByAlarmManager() {

    Calendar cur_cal = Calendar.getInstance();
    cur_cal.setTimeInMillis(System.currentTimeMillis());
    cur_cal.add(Calendar.SECOND, 50);
    long backgroundServiceUpdateInterval = 0;


       Interval = 3 * 60 * 1000; // xyz

   // stopService(new Intent(this,cl));
    // Background sync service
    Intent mServiceIntent = new Intent(this, cl);
    mServiceIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
    PendingIntent pintent = PendingIntent.getService(this, 121,
            mServiceIntent, PendingIntent.FLAG_UPDATE_CURRENT);

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

    alarm.setRepeating(AlarmManager.RTC_WAKEUP, cur_cal.getTimeInMillis(),
            Interval, pintent);
}
Farhan Ibn Wahid
  • 926
  • 1
  • 9
  • 22
Praveen Mishra
  • 146
  • 1
  • 12

1 Answers1

-1

NOTE : Applicable on Task manager apps killer ONLY

  1. Setup MIUM security issues.

For MIUM 7.0 Security => Autostart => select apps that you want to run in background services => reboot

For MIUM 4.0 settings

  1. MainActivity.class (start services)

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(this, Your_IntentServices.class);
    
        startService(intent);
       }
    }
    

    Your_IntentServices.class (you could extends IntentService)

    public class Your_IntentServices extends Service {
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    

    Menifest (add service)

    <manifest>
        <application>
        <service
            android:name=".Your_IntentServices"
            android:exported="false"/>
        </application>
    </manifest>
    

More MIUM Discussion / Other solution

Community
  • 1
  • 1
Txuver Jhi Wei
  • 318
  • 2
  • 5
  • 17