I have a service ongoing which takes current location and sends it to the server.
While App is in the background with Service on, if I go to settings and turn off my App Location, the app is crashing I'm unable to able to catch the crash.
But if I kill the app and re-run it again the crash does not happen at that time.
It only happens when the app is in the background and at the same time app location permission is turned off.
Thanks & Regards
Syed
You could register for the LocationManager.PROVIDERS_CHANGED_ACTION in the BroadcastReceiver. When it is hit, you can check if the provider is still enabled and use that accordingly to prevent your app from crashing when someone revokes the location permission.
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
boolean enabled = locationManager != null && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Quoting
I mean if i have app in background and i turn my app location
permission off then if i open my app from background it crashes at
that time only. If i kill and open my app then it works fine. Im not
able to produce Exception/Error logs
The behavior you are observing is correct. You need to explicitly check for the Location Permission before registering/starting location update listener.
Related
I have written a foreground service in my app and I am doing some API operation every 5 minutes once. My app is mainly used for location tracking and it always needs location permission. If user disable the location for my app by any chance then I just want to create a local notification from my service. My app users mostly don't bring my app to foreground. So I want to check whether the location permission is enabled or not. If not then I just want to show a notification which tells the user that the app needs location permission. Now I just manually went to android settings and disabled the location permission for my app. I have two services running in my app. Suddenly both stopped working. I just want to know the reason why it stopped? Will it be stopped always until I enable the permission? What can I do to intimate my user to enable the permission? As my app always runs in background, I want to handle it in service. Please give your opinion to solve my issue.
NullPointerException: Attempt to read from field'LanguageInitBean$Data LanguageInitBean.data' on a null object reference
When I change the storage permission, I can't read the data stored in memory
Ask for help
public static class LanguageInitCache{
public static LanguageInitBean languageInitBean;
}
private void startToNext(String response) {
languageInitBean = GsonUtils.json2Bean(response, LanguageInitBean.class);
}
I am using android location sample code "LocationUpdatesPendingIntent" link for requesting location updates even when the application is killed. It works very well in android 6, I don't turn on GPS in the device even then it starts showing me location but poor accuracy(that is acceptable). But in lollipop 5.1 it doesn't start and doesn't show any notification. Because it is getting null LAT and LONG. But it workes well even in lollipop 5.1 if I turn on GPS.
My Q is how to add some callback in:
mFusedLocationClient.requestLocationUpdates(mLocationRequest, getPendingIntent());
to check if it is able to get location LAT LONG or not. In lollipop case, it doesn't even fire broadcast.
I have developed a Android hybrid app that amongst other activities tracks the users location and displays it to the user showing where they have been recently. This works by invoking LocationServices.FusedLocationApi.requestLocationUpdates at a 10 second update rate.
Obviously this requires location permission and for ANDROID Marshmallow and above I check and request the necessary location privileges from the user before requesting location updates.
I have read many other StackOverflow question about the same topic but I have not found a satisfactory answer about preventing Android from terminating the app if location permission is revoked for the app by the user after initially granting it.
I would be more than happy for the app to stop displaying the user's location and track, after all the user has asked for it to be stopped, but I am far less happy for the app to be terminated, as I said earlier the app does many other things for the user whilst running in the background.
As far as I understand there is no callback that can be used to allow me to remove the location updates as soon as the location permission is revoked. How could I change my strategy to avoid the app crashing?
It is not that I am expecting users to do this very often, but I think it good practice to make the app as bulletproof as possible. Please do not remind me that Android can terminate any app in the background as that is not my question here.
Do I need to adopt a singular rather than a periodic approach to requesting the location so that I can re-check the permission exists each time.
Or is it better to try and catch the error. If so where do I do this? Would it be inside onLocationChanged?
What I mean by app crash is that the process is terminated (I have checked and this actually happens)
That's perfectly normal, when the user revokes a runtime permission.
What I would like to do is prevent the process terminating if there is any way I can do this.
Sorry, that is not possible.
Since I suspect that few users even know that they can revoke runtime permissions, this is not going to be a common occurrence, IMHO.
Blockquote
however why did the Android developers not just cause a callback to be generated to allow the app code base to react
You could register for the LocationManager.PROVIDERS_CHANGED_ACTION in the BroadcastReceiver.
When it is hit, you can check if the provider is still enabled and use that accordingly to prevent your app from crashing when someone revokes the location permission.
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
boolean enabled = locationManager != null && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
In my app I am using Android Location Manager API and I have a requirement of identifying whether the location coordinates are from normal Location Manager or if they are from Fake(Mock) Location provider. I used location.isFromMockProvider(). Once I turn on the fake location providing app, isFromMockProvider() method returns me true. And after that it does not matter if I turn, fake location providing app, on or off isFromMockProvider() method always returns true.
I found that once FakeGPS app, once triggered, overrides the location manager coordinates and once I stop the FakeGPS app, the Location Manager does not start collecting correct coordinates.
Is there something I am missing.
Disappointed with no response but I am glad that I got it working :)
Posting the resolution as it might help someone else.
Once the FakeGPS app starts mocking location, it stops my app's Location manager.
Now even if I stop the FakeGPS app mocking service, the location manager of my app won't trigger back.
I have to detect the event and restart the location manager of my app, after removing any test provider and clearing test locations.
just resetting and removing test location should help
String t = LocationManager.GPS_PROVIDER;
locationManager.clearTestProviderEnabled(t);
locationManager.clearTestProviderLocation(t);
locationManager.clearTestProviderStatus(t);
locationManager.removeTestProvider(t);
I would like to enable the location settings on an android machine. In my program I check if the
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
..//
}
If deactive, I would like to activate it programatically.
EDIT: When my location settings are disabled and I call/use googleMaps, I am asked by means of a dialog, whether I want to enable it? If "Yes", the location service is being enabled in the background without callings another user interaction.
Can I enable location settings without having other user interactions?
No you cannot. location information is build into the system, which require permission. Android M currently has "permission" build for each app that can set it when app starts. But if user forcefully turn off GPS then the user has to turn it back on him/herself.