Android app stop after running in background by using foreground service?

Welcome to Android Central! Which phone? Which app?
 
Last edited:
Android app stop after running in background by using foreground service

In my app I have used foreground service for getting location it works fine for some interval. When the app is in the background, then location not received and the app gets killed without showing any error log. App works fine in some devices.here is my code Start service from Activiy :

startServiceIntent = new Intent(this, MyLocationService.class);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

ContextCompat.startForegroundService(this, startServiceIntent);
} else {

startService(startServiceIntent);
}

MyLocationService:

@override
public int onStartCommand(Intent intent, int flags, int startId) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(CHANNEL_ID,"Channel",NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(serviceChannel);

Intent notificationIntent = new Intent(this, HomeActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("My App")
.setSmallIcon(R.drawable.logo)
.setContentIntent(pendingIntent)
.build();

startForeground(1, notification);
}
return START_NOT_STICKY;
}

private void geCurrentLocationOfUser() {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

return;
}
}
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();

provider = locationManager.getBestProvider(criteria, false);
locationManager.isProviderEnabled(provider);



if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, this);
mCurrentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

if (mCurrentLocation != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(mCurrentLocation);
} else {
System.out.println("Provider " + provider + " Location not available.");
}
}
 
Threads merged. I will also move this to the Software Development forum for more specific traffic.
 
when your app was kill, your notification is still here? you should try to use adb shell dumpsys meminfo to find you packageName is still in the "Foreground" tag, if yes some rom may has logic to clean some process which excussive power consumption, as you service is using gps
 

Forum statistics

Threads
954,076
Messages
6,960,500
Members
3,162,919
Latest member
nati