0

This has been asked so many times, I have seen and applied many answers but nothing seems to be helping here.

This is how i am starting server on application launch

startService(new Intent(MavsMainActivity.this, LocationUpdateService.class));

Manifest

<service android:name=".myservices.services.LocationUpdateService"
    android:process=":locationService"
    />

When i forcefully closed the application, It triggers the onCreate() of service again. I have tried return START_STICKY and return START_NOT_STICKY, one re-starts the service on Application onDestroy, and one restarts it when user relaunch the application after destroying it respectively.

This is my service class, Kindly guide me how can i keep service running on background even user destroys the application.

        @Override
        public void onCreate() {

            if (isGooglePlayServicesAvailable()) {
                /* min dist for location change, here it is 10 meter */
                mGoogleApiClient = new GoogleApiClient.Builder(this)
                        .addApi(LocationServices.API)
                        .addConnectionCallbacks(this)
                        .addOnConnectionFailedListener(this)
                        .build();

                mGoogleApiClient.connect();
                Toast.makeText(getApplicationContext(), "Service created", Toast.LENGTH_SHORT).show();

    }


        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            Log.i(TAG, "onStartCommand: ");
            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(BACKGROUND_INTERVAL);
            mLocationRequest.setFastestInterval(30000);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
            mLocationRequest.setSmallestDisplacement(20);

            sendOfflineData();
            Toast.makeText(getApplicationContext(), "onStart Called", Toast.LENGTH_SHORT).show();

   return START_STICKY ;

    }
dev90
  • 7,187
  • 15
  • 80
  • 153

1 Answers1

3

You have to use IntentService instead of Service why?

Service: The Service runs in background but it runs on the Main Thread of the application.

IntentService: The IntentService runs on a separate worker thread.

On Application Destroy service stop and the restart it if you return START_STICKY because its seprate thread but running on Main application thread.

you should IntentService and look here for details.

Community
  • 1
  • 1
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
  • Thanks, This make things clearer, One Question, not related to this question, will be grateful if you answer it here. I have extends service from IntentService, and it creates 2 additional methods, `onHandleIntent` and a constructor, Do i need to do anything in them, or will it work fine without giving any implementation to these methods. – dev90 Feb 01 '17 at 07:11
  • In case of IntentService, implement your logic (code) into onHandleIntent method. – Rikin Prajapati Feb 02 '17 at 13:38