2

I wonder if I can keep the app service running inspite of the app is swiped out or closed. I want to send the gps data to the server all the time whether the app is opened or in the background or is closed (swiped out from the recent app list etc). I've tried 'service' but is not working. How can I do it. Can sb give me some hints on it please? Thankyou.

public class MainActivity extends AppCompatActivity{

    BroadcastReceiver mReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startService(new Intent(getBaseContext(),TestService.class));
    }

}

public class TestService extends Service {

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("service", " destroyed");
        Toast.makeText(this, "Service destroyed", Toast.LENGTH_SHORT).show();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(this, "service started", Toast.LENGTH_SHORT).show();
        Log.e("service", " onStartCommand");
        return START_STICKY;
    }


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
Amrita Stha
  • 2,973
  • 3
  • 25
  • 46
  • 1
    You might want to look into the `JobScheduler` API: https://developer.android.com/reference/android/app/job/JobScheduler.html – Simon Schiller Sep 01 '17 at 06:21

1 Answers1

2

You can use

  1. JobScheduler

This API allows you to run scheduled service and the android system will batch all the services from different applications and run them together in some particular timeframe

  1. Firebase Job Dispatcher

The Firebase JobDispatcher is a library for scheduling background jobs in your Android app. It provides a JobScheduler-compatible API that works on all recent versions of Android (API level 9+) that have Google Play services installed.

for reference you can use following links

reference 1
reference 2
reference 3
referennce 4
reference 5
reference 6

Ravi
  • 34,851
  • 21
  • 122
  • 183