'Android 11 app stop running when the user puts it into background, but it work on previous version
Android As you can see bellow code. I tried to get background location accessing with Service but does not working on android 11 when app pause but it's work on previous android version.
public class GoogleService extends Service { static final int LOCATION_SERVICE_ID = 1; static final String ACTION_START_LOCATION_SERVICE = "START"; static final String ACTION_STOP_LOCATION_SERVICE = "STOP"; public static boolean isServiceRunning; double latitude, longitude; Context context;
public GoogleService() { this.context = GoogleService.this; } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); } private LocationCallback locationCallback = new LocationCallback() { @Override public void onLocationResult(LocationResult locationResult) { super.onLocationResult(locationResult); if (locationResult != null && locationResult.getLastLocation() != null) { latitude = locationResult.getLastLocation().getLatitude(); longitude = locationResult.getLastLocation().getLongitude(); Log.d("Coordinates", latitude + "," + longitude); Toast.makeText(context, latitude + "," + longitude, Toast.LENGTH_SHORT).show(); } } }; public void startLocationService() { String channerId = "Location Notification channel"; NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Intent intent = new Intent(); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(),PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channerId) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("Location Service") .setDefaults(NotificationCompat.DEFAULT_ALL) .setContentText("Running") .setContentIntent(pendingIntent) .setAutoCancel(false) .setPriority(NotificationCompat.PRIORITY_MAX); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (notificationManager != null && notificationManager.getNotificationChannel(channerId) == null) { NotificationChannel notificationChannel = new NotificationChannel(channerId, "Location Service",NotificationManager.IMPORTANCE_HIGH); notificationChannel.setDescription("This channel use by location service"); notificationManager.createNotificationChannel(notificationChannel); } }
LocationRequest locationRequest = new LocationRequest() .setInterval(100) .setFastestInterval(200) .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return; } LocationServices.getFusedLocationProviderClient(this) .requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper()); startForeground(LOCATION_SERVICE_ID,builder.build()); } public void stopLocationService() { LocationServices.getFusedLocationProviderClient(this) .removeLocationUpdates(locationCallback); stopForeground(true); stopSelf(); } @Override public IBinder onBind(Intent intent) { throw new UnsupportedOperationException(""); } @Override public int onStartCommand(Intent intent,int flags,int startId) { if(intent != null) { String action = intent.getAction(); if(action != null) { if(action.equals(ACTION_START_LOCATION_SERVICE)) { startLocationService(); }else if(action.equals(ACTION_STOP_LOCATION_SERVICE)) { stopLocationService(); } } } return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { isServiceRunning = false; super.onDestroy(); } }
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
