FreakyJolly.com
FreakyJolly.com copied to clipboard
How about the app is in background
Right now the implementation is in activity. What happens when the app goes to the background ? as per new Android Performance Rules, the device wouldn't receive continuous location updates in the background
@nidhinprathap we can do so by using a service or binding a broadcast to location service, as per new Android updates, to make OS more secure they may limit service threads for background activities but broadcasts will work fine.
Recently I have developed a service class that can run when the app is running onPause. But still now it is not working when the app is killed. I think this line of code will help `private void startMyOwnForeground(){ String NOTIFICATION_CHANNEL_ID = "com.project.nirjan.location.geotracker"; String channelName = "channel2019"; NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE); chan.setLightColor(Color.BLUE); chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); assert manager != null; manager.createNotificationChannel(chan); String s = "Getting location. Please wait "; if (jo != null){ try { s = getString( R.string.notification_text, jo.getDouble(Config.LATITUDE), jo.getDouble(Config.LONGITUDE), jo.getDouble(Config.ACCURACY)
);
} catch (JSONException e) {
e.printStackTrace();
}
}
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.setContentText(s)
.setContentIntent(pendingIntent)
.build();
// Intent notificationIntent = new Intent(getApplicationContext(),MainActivity.class); // notification.contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, notificationIntent, 0); startForeground(2, notification); }`
In a foreground service, i'm using FusedLocationProviderClient to request for location updates. I'm using foreground service because, i want location updates even when the app is closed. so far so good. it is working. But the problem is, if the gps is turned off then i'm not getting the onLocationResult() call back. How to handle this problem ?? Before starting the service, location was on and i got a couple of callbacks in onLocationResult() method. After sometime, user turned off the gps, after that i'm not getting the onLocationResult() call back. Any idea how to deal with this problem ??
Here is the code
' val mFusedLocationClient: FusedLocationProviderClient =
LocationServices.getFusedLocationProviderClient(this)
val mLocationCallback: LocationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
// location is received
val locationList = locationResult.locations
for (mCurrentLocation in locationList) {
Log.d("log", "current Location = ${mCurrentLocation.latitude}, ${mCurrentLocation.longitude}")
}
}
}
val locationRequest = LocationRequest()
locationRequest.interval = (5 * 60 * 1000).toLong()
locationRequest.fastestInterval = (5 * 60 * 1000).toLong()
locationRequest.maxWaitTime = (15 * 60 * 1000).toLong()
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
mFusedLocationClient.requestLocationUpdates(locationRequest, mLocationCallback, Looper.myLooper())`
Tried with both PRIORITY_HIGH_ACCURACY and also with PRIORITY_BALANCED_POWER_ACCURACY