Android GPS My Location finding - android

I'm using this code to find my current location. But if I'm at home it can't locate me. But google maps and another similar applications can locate me although I'm at home. What is difference?

If you're using the GPS_Provider, make sure that you have the following permission in your app manifest:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
But usually GPS only works with a free sky (not indoors very well). Therefore while Google Maps might show the last known location, maybe your app doesn't. So you might want to initially call getLastKnowLocation initially to set the initial marker.
Even though GPS cannot get a fix, you could still show the last known location known to the device, i.e. when you were outdoors last time.
Alternatively or additionally to GPS_Provider, you could also use the
LocationManager.NETWORK_PROVIDER
instead,which is based on Wifi and/or GMS, is less accurate than GPS but works indoors.
For this, you need the permission
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
in your manifest.

Related

What are the requirements to comply with the background location rules from the Play Store?

I have a similar issue to this post How to make my Android app comply with the "Background Location Policy" but that post doesn't have an answer.
I have a web browser app, which on occasion will ask for location permissions if the user visits a website that requests that. App targets API 29. The app manifest has <uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION" /> which is API 23 and higher because I didn't want to force location permission on older phones.
Today I got an email saying I have until March to fix this, but I don't understand what I have to do, I'm not requesting background location anywhere.
Anyone have any idea what I have to do?
Edit: I have read the help center, and I'm wondering if that my issue is the ACCESS_FINE_LOCATION, maybe something is accessing it on the background. I don't know how I would prevent ad networks from that if they do it. I already pause the WebViews the app is not on the foreground so websites should not be using it.
Edit2: Is there maybe a way I can log background location access so that I can monitor my app a few days to see if it happens?
I would like to answer my own question in case someone else is searching for this. I can't say that I know for certain this is correct but it has worked for me so far.
First, on this answer https://stackoverflow.com/a/65894488/704836 I was told to use AppOpsManager to log background location requests. After doing that I found a few places where that takes places. I will discuss those below:
Ad networks. I have ad network initialization on my Application.onCreate() and a lot of those accessed location. So when triggering a BroadcastReceiver, they would check location.
WifiManager.getConnectionInfo() - this will trigger a location request. Same deal as above, I had one of those on Application.onCreate().
After removing those calls the Play store stopped complaining.

How to avoid getting null on user location result in android?

In my app I am using FusedLocationProviderClient to get user location every time he enters the app by mFusedLocationClient.getLastLocation().addOnCompleteListener().But sometimes result is null,I am using <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> in manifest.How can I get user location even if it not so correct?Because the logic of app depends on that location,I don't need to show it on map or smth like that,just get the location.Thx in advance
Don't use getLastLocation(). There is always a chance to have it return null if it doesn't have a location currently.. Use requestUpdates or requestSingleUpdates- when the callback for those is called the location is guaranteed to be non-null. getLastLocation() should only be used as an optimization to improve speed, it cannot be relied on.
Follow the following example and your app is guaranteed to work as you wish. I have recently used this code and it is working like a charm.
Only thing you should know is that for the location updates to actually work ( to get real latitude and longitude values) you should enable "google play services". There may be several ways to do this but perhaps the simplest of all is to open "settings" on your device and probably somewhere under "security" or "permissions" (depending on your android distribution) find "google play services" and give it the permission it needs.
https://github.com/googlesamples/android-play-location/tree/master/LocationUpdates

Check if Location is unavailable in Android Location Client

I would like to check if Location Client is able to provide any location. Is there any way to do so, apart from calling normal location system service?
The accepted answer was correct at the time. But in recent times things have changed with the availability of the SettingsApi It allows you to check if the location services are available on the device and there is a complete example provided by google on github that shows how to use this API. The sample code will prompt the user to enable location if it's switched off.
AFAIK, there is no method to check availability.
Possible Reason:
LocationClient uses fused API which gives location with appropriate combination of GPS,WiFi,Cell,Sensors (according to the Priority you have set for retrieving location).
Reference:
Fused API - Beyond the blue dot
Alternate Solution:
You already know it.you can check availability of provider by using location system service and LocationManager

Force Proximity Alert to use Network Provider

Is there any way to force proximity alerts to use only network provider?
I'm worried about battery drain.
Forcing proximity alerts to take more than 4 minutes to be fired would be a good solution too.
Any ideas?
A brilliant question,sadly though I dont think that would be possible , just had a look over the documentation as well , take note of this "Before API version 17, this method could be used with ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION. From API version 17 and onward, this method requires ACCESS_FINE_LOCATION permission." So they are making it compulsory to pass in GPS Permission.
So currently there is no way of forcing it to use any particular provider.
There is a Enhancement Feature requested for the same , go ahead and star it to make Google implement it.
Link to Enhancement : https://code.google.com/p/android/issues/detail?id=33151
If your project does not require gps for other features you can set the SDK target to version 16:
android:targetSdkVersion="16"
and require just the coarse location:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
so the proximity alerts will not check for the gps signal.
My point is that is the user keeps the GPS ON is conscius of the drain, remember that this is a toggle, you should consider it in the requirement of the app.

Replace default GPS signal provider on Android devices

I would like to replace the default GPS location provider on android phones with my own coordinate source.
Is there any layer/API/library/provider that would allow to be replaced with my signal provider instead of the built-in hardware GPS, or read from an external plugin? It should allow all apps that rely on the GPS service to receive my signal instead of GPS.
Ideally, the replacement should still be able to access the GPS signal (for comparison/correction or to toggle between the two providers).
I am thinking for example of implementing my own LocationManager, and registering it in the system (as optional or default), if that is possible. But at this stage, I am still trying to find out what is possible and suitable.
Thank you for any pointers.
Replacing the "default" is not allowed at this time and the only work around is to create a mock provider. First you must set the security permission:
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
Second, set your code to use the mock provider which you will create. There is some excellent information about how to accomplish this here: http://diffract.me/2009/11/android-location-provider-mock/
I'm only in the process of reading about the android SDK at the moment. However my understanding is that you would provide an implementation of a GPS_PROVIDER intent.
Any application can then divert to using the user preferred GPS_PROVIDER intent.
See:
http://developer.android.com/guide/topics/intents/intents-filters.html
I'm pretty sure the default location providers for fine location are baked into Android.
Given that Google are becoming more conscious about potential misuse and abuse of system resources by applications, i'd say that's a good thing. Otherwise people would be able to trick the user into thinking they're somewhere else, using the trustworthy sounding 'Use GPS satellites' setting.
The only way I can see that you can add a location provider is via android.location.LocationManager.addTestProvider(......), which requires the permission ACCESS_MOCK_LOCATION. Now, this method and permission are both documented as being for "testing" purposes. I can't find a source that specifically says that that permission is not granted for market release apps, but I strongly suspect that to be the case.

Categories

Resources