0

My service is being killed by os when I kill application which I don't want . I want to run background service for some job. Please guide me how can I do this.

Here is service class code:

public class MyThreadClass extends Service {
    Handler handler;
    Runnable runnable;
    private static final String TAG = "MyThreadClass";
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;

    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        handler=new Handler();
      runnable=new Runnable() {
       @Override
       public void run() {
           Log.d(TAG, "run: Runnging in Background");
           handler.postDelayed(this,1000);
       }
   };
   handler.postDelayed(runnable,1000);
   return START_STICKY;

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: Called");
        //handler.removeCallbacks(runnable);
    }
}

manifest declaration :

<service android:name=".MyThreadClass"></service>

please guide me how can I run my service on background properly thanks

user9706969
  • 37
  • 1
  • 6
  • I didn't code for startForground because I don't know too much about service I am new one to android – user9706969 Jun 29 '18 at 10:17
  • 1
    Are you Having Target API level version higher than 26? if yes then it is compulsory to make service in the foreground because the system imposes restrictions on running background services when the app itself isn't in the foreground. – Gaurang Goda Jun 29 '18 at 10:37
  • Please check this link - https://stackoverflow.com/questions/9740593/android-create-service-that-runs-when-application-stops – Android and Security Guru Jun 29 '18 at 10:59
  • yes I am targeting API 27 but does it mean I can't use service for background job? – user9706969 Jun 29 '18 at 11:02
  • I need unending background service but when I need to stop, I can stop through any button something like that – user9706969 Jun 29 '18 at 11:04
  • There is no longer such a thing as an unending background service. Either make it a foreground service or switch to jobs. – Sander Jun 29 '18 at 11:28
  • Then how can I check my nearby locations around after 5 minutes when I am travelling please suggest something – user9706969 Jun 29 '18 at 11:37

1 Answers1

0

You can use CountDownTimer, below is the code for same, hope it helps.

public class MyThreadClass extends Service {
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        CountDownTimer countDownTimer = new CountDownTimer(5000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                //calls after every 500ms
                // do something
            }

            @Override
            public void onFinish() {
                //calls after finishtime 5000ms
                //do something
            }
        };
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: Called");

    }
}
Gaurang Goda
  • 3,394
  • 4
  • 20
  • 29