Reputation: 13
i have an app that detects the location in the background. In the devices with android 9 (api 28) or lower it works, but in android 10 it stops after 20 sec
there is some code
how i call the service
Intent intent = new Intent(getApplicationContext(), GoogleService.class);
startService(intent);
main activity
@Override
protected void onResume() {
super.onResume();
registerReceiver(broadcastReceiver, new IntentFilter(GoogleService.str_receiver));
}
@Override
protected void onPause() {
super.onPause();
registerReceiver(broadcastReceiver, new IntentFilter(GoogleService.str_receiver));
}
@Override
protected void onDestroy() {
super.onDestroy();
registerReceiver(broadcastReceiver, new IntentFilter(GoogleService.str_receiver));
}
google service
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mTimer = new Timer();
mTimer.schedule(new TimerTaskToGetLocation(), 5, notify_interval);
intent = new Intent(str_receiver);
}
Upvotes: 1
Views: 1564
Reputation: 20129
As CommonsWare noted, running a background service like this will result in the system killing it after the user leaves your app on newer versions of Android.
Depending on the exact characteristics of how you're trying to do this, you should either use WorkManager (if this is deferrable and not needed more than every 15 minutes or so) or a foreground service with a notification (if you need to query constantly). See this guide from the Android documentation for more information on your options here.
Upvotes: 1