2
public class MyService extends Service {

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

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

        super.onStartCommand(intent, flags, startId);

        Log.e("Service", "Running");

        return Service.START_STICKY;
    }
}

My service is killed and didn't restart after task kill in vivo phones. But, Facebook app restart it's service in my phone. Why? public class StartMyServiceAtBootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {

        Intent myService = new Intent(context, MyService.class);
        context.startService(myService);
    }
    else if(Intent.ACTION_USER_PRESENT.equals(intent.getAction())) {
        Intent myService = new Intent(context, MyService.class);
        context.startService(myService);
    }

    }

} I start my service by using receiver..

StuckPixel
  • 111
  • 8

1 Answers1

0

You are not doing anything in your service, if you are not doing any thing it obviously gets Garbage collected,

try

while(true) {

Log.d(TAG, "testing") 

}

in that service onStartCommand function, that should keep it running always

I would also recommend starting a Thread in side onStartCommand when OS kills the service, automatically service gets restarted.

    new Thread(new Runnable() {
        @Override
        public void run() {

            while (isRunning) {
             }
        }
    }).start();
Aravind.HU
  • 9,194
  • 5
  • 38
  • 50
  • I do my task inside onStartCommand function...But it`s ok in samsung phones but in funtoch OS ,Task killer kill both my process and service.My service cannot restart.Why should I do? – StuckPixel Mar 04 '16 at 11:22
  • How are you starting the service, can you place the code for that – Aravind.HU Mar 04 '16 at 11:35