I am making an Android App which includes google map. While minimizing the app no longer searches for GPS. Is there a way to turn on the gps for the app all the time even when the map is minimized?
Asked
Active
Viewed 256 times
0
-
To run your GPS location continuously use service class. [Look at this solution](https://stackoverflow.com/a/21532572/9254960). If u want the app run after app close. run the service continuously else, kill the service at `onTaskRemoved(Intent rootIntent)` inside service class with `stopSelf()` method. – Chethan Kumar Oct 26 '18 at 06:18
-
Sounds like it will work, I am going to apply this and coming back to you with an answer – Sudipta Patra Oct 26 '18 at 06:23
1 Answers
0
@Sudipta for prior Oreo version you have to use jobscheduler.
- Create a JobScheduler class
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class JobSchedulerClass extends JobService {
@Override
public boolean onStartJob(JobParameters params) {
return false;
}
@Override
public boolean onStopJob(JobParameters params) {
Intent startService = new Intent(this, YourServiceClass.class);
ContextCompat.startForegroundService(this, startService);
return false;
}
}
- Call the service class and service class based the the version of device running
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
JobInfo jobInfo = new JobInfo.Builder(11, new ComponentName(context, JobSchedulerClass.class))
.setRequiresBatteryNotLow(false)
.setMinimumLatency(100)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_NONE)
.build();
assert jobScheduler != null;
jobScheduler.schedule(jobInfo);
} else {
Intent startService = new Intent(context, YourServiceClass.class);
context.startService(startService);
}

Chethan Kumar
- 185
- 1
- 12
-
Sorry, for time being, i have used my local class names in the project. replace MainActivity.class to your service class.... updated the post. please check now – Chethan Kumar Oct 26 '18 at 08:10