Enabling gps on android

Android Turn on GPS programmatically

Jan 2, 2019 · 3 min read

In our previous article, we have taught you how to get location using FusedLocationProviderClient and we also mentioned in that article that for using this feature, you need to turn on device GPS manually by redirecting to settings of your device.

Programmatically we can turn on GPS in two ways. First, redirect the user to location settings of a device (by code) or another way is to ask to turn on GPS by GPS dialog using LocationSettingsRequest and SettingsClient.

First method:
By usin g this, the user will be redirected to the Settings page of your device and user has to turn on GPS manually.

Second method:
In this method, we will show you code to turn on GPS using android gms location request and setting clients. Here we provide a custom class GpsUtils.java with a code written in it with method turnGPSOn(). The method has a callback listener to check the current status of GPS. If GPS is already turned on, no further code will be determined and callback revert true as GPS is on. Let’s look at the code below and we will show how to use GpsUtils.java.

SettingsClient class is the main key point for interacting with location-based API. This API makes it easy for an app to ensure that the device’s system settings are properly configured for the app’s location needs.

LocationSettingsRequest Specifies the types of location services the client is interested in using. Settings will be checked for optimal functionality of all requested services. Use LocationSettingsRequest.Builder to construct this object.

GPS Dialog will be shown for SettingsClient’s failure callback and it will result in an activity’s onActivityResult(). So basically if the user agrees for turn on GPS, we can enable GPS flag in our activity.

and now call GpsUtils class before getting a location.

We recommend you to put this code in onCreate() of your activity. So that, turn on GPS and getting a location works well one by one. And also GPS takes some minor delay to trace your device location.

We have provided code in the previous article to get a location. Thus, we made changes for GPS in that code and republish the same code.

Читайте также:  Onresume android примеры использования

Find code here. Code contains previous article example for getting the current location.

Источник

gps.conf для всех, или как ускорить работу GPS на Android

«Фантастика!» подумал я и без промедления перешел по ссылке. По сравнению с первым постом в этот раз предлагались еще более конкретные действия, а именно заменить содержимое файла gps.conf (его можно найти по пути /etc/gps.conf, должны быть root-права) на следующие настройки:

NTP_SERVER=ua.pool.ntp.org
NTP_SERVER=0.ua.pool.ntp.org
NTP_SERVER=1.ua.pool.ntp.org
NTP_SERVER=2.ua.pool.ntp.org
NTP_SERVER=3.ua.pool.ntp.org
NTP_SERVER=europe.pool.ntp.org
NTP_SERVER=0.europe.pool.ntp.org
NTP_SERVER=1.europe.pool.ntp.org
NTP_SERVER=2.europe.pool.ntp.org
NTP_SERVER=3.europe.pool.ntp.org
XTRA_SERVER_1=/data/xtra.bin
AGPS=/data/xtra.bin
AGPS=http://xtra1.gpsonextra.net/xtra.bin
XTRA_SERVER_1=http://xtra1.gpsonextra.net/xtra.bin
XTRA_SERVER_2=http://xtra2.gpsonextra.net/xtra.bin
XTRA_SERVER_3=http://xtra3.gpsonextra.net/xtra.bin
DEFAULT_AGPS_ENABLE=TRUE
DEFAULT_USER_PLANE=TRUE
REPORT_POSITION_USE_SUPL_REFLOC=1
QOS_ACCURACY=50
QOS_TIME_OUT_STANDALONE=60
QOS_TIME_OUT_agps=89
QosHorizontalThreshold=1000
QosVerticalThreshold=500
AssistMethodType=1
AgpsUse=1
AgpsMtConf=0
AgpsMtResponseType=1
AgpsServerType=1
AgpsServerIp=3232235555
INTERMEDIATE_POS=1
C2K_HOST=c2k.pde.com
C2K_PORT=1234
SUPL_HOST=FQDN
SUPL_HOST=lbs.geo.t-mobile.com
SUPL_HOST=supl.google.com
SUPL_PORT=7276
SUPL_SECURE_PORT=7275
SUPL_NO_SECURE_PORT=3425
SUPL_TLS_HOST=FQDN
SUPL_TLS_CERT=/etc/SuplRootCert
ACCURACY_THRES=5000
CURRENT_CARRIER=common

Эти настройки рассчитаны на жителей Украины, но для жителей России их очень легко адаптировать заменив ua.pool на ru.pool.

Далее нужно перезагрузить Android, а затем запустить программу для работы с GPS и наслаждаться стабильным сигналом.

От себя могу добавить, что я пользуюсь приложением GPS Status и после перезагрузки при первом запуске приложения я сделал сброс данных кэша: в программе вызываем меню, далее выбираем Tools, там Manage A-GPS state и жмем Reset, а затем Download.

Снимаю шляпу перед автором оригинального поста, уважаемым mechanicuss. Его совет помог не только мне, и может помочь еще многим страдающим.

На этом все. Всем чистого неба и стабильного сигнала из космоса.

Источник

How to turn on the GPS on Android

I am developing an android app which needs to activate the GPS.

I read a lot of topics in a lot of forums and the answer I’ve found is:

But. the «Cerberus» APP turns my GPS on. so. it’s possible!

Can anyone help me with this?

7 Answers 7

No, it’s impossible, and inappropriate. You can’t just manage the user’s phone without their authority. The user must interact to enable GPS.

From Play Store:

«Cerberus automatically enables GPS if it is off when you try to localize your device (only on Android

I think we have more better version to enable the location without opening the settings just like google map works.

It will looks like this —

Add Dependency in gradle — compile ‘com.google.android.gms:play-services-location:10.0.1’

There used to be an exploit that allowed the GPS to be turned on by an app with no special permissions. That exploit no longer exists as of 2.3 (in most ROMs). Here’s another post that talks about it,

Читайте также:  Adb program android что это такое

«GPS enabled» is a secure setting, so you must have WRITE_SECURE_SETTINGS permission. This is a signature protected permission however, so you app will not be granted this unless it is signed with the manufacturer’s platform certificate.

The correct thing is to send the user to the location settings page, and let them enable GPS if they wish. e.g.,

This creates an alert and allows the user to go to the settings screen and hit the back button to come right back to your app. The power widget exploit doesn’t work beyond 2.3 to my knowledge.

Use this code //turnGPSON called After setcontentView(xml)

you might want to check out this thread

here are the codes copied from that thread

but the solution is not recommended as it will not be available to android version > 2.3 supposingly.. do check the comments

The GoogleApiClient has been deprecated, we need to use SettingsClient and GoogleApi to enable GPS without going to the Location Settings just like Google Maps, OLA, Zomato etc. Below code supports any of the Android Version starting from 4.4 or lower to 11+

dependency required in gradle file:

To check GPS state ON or OFF:

If GPS is already enabled then we will display toast notification otherwise will ask to turn the GPS on.

If GPS is not ON, then flow will come to RESOLUTION_REQUIRED and that will call startResolutionForResult which will be handled by onActivityResult

Now, add onActivityResult method —

If resultCode is RESULT_OK that means user allowed to turn GPS on else for RESULT_CANCELED, you can again ask or show the rationale dialog.

  • You can wrap above code in method and call wherever you require
  • While building LocationRequest you can set Interval, Priority, SmallestDisplacement, FastestInterval, etc as per you required by your app.
  • We have used Task api instead of PendingResult

Источник

How do I find out if the GPS of an Android device is enabled

On an Android Cupcake (1.5) enabled device, how do I check and activate the GPS?

11 Answers 11

Best way seems to be the following:

In android, we can easily check whether GPS is enabled in device or not using LocationManager.

Читайте также:  Обязательное обновление андроид что это такое

Here is a simple program to Check.

GPS Enabled or Not :- Add the below user permission line in AndroidManifest.xml to Access Location

Your java class file should be

The output will looks like

yes GPS settings cannot be changed programatically any more as they are privacy settings and we have to check if they are switched on or not from the program and handle it if they are not switched on. you can notify the user that GPS is turned off and use something like this to show the settings screen to the user if you want.

Check if location providers are available

If the user want to enable GPS you may show the settings screen in this way.

And in your onActivityResult you can see if the user has enabled it or not

Thats one way to do it and i hope it helps. Let me know if I am doing anything wrong.

Источник

How to programmatically enable GPS in Android Cupcake

I’m currently writing an app in Android that works with the GPS. At the moment I’m able to work out whether the GPS is enabled. My problem is that I want to enable the GPS on app startup if it is disabled. How can I do this programmaticaly?

7 Answers 7

You can’t, starting with Android 1.5. The most you can do is pop open the activity to allow the user to toggle it on/off. Use the action held in android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS to craft an Intent to open this activity.

This method code can be help for you

You might use the following:

but it will only work if you have system signature protection level. So you need to cook your own Image to actually use it :/

First check whether the location service is already on or not ??

Checking location service is enabled or not

Then finally to open if location service in turned off previously

You should use the Location Settings Dialog in Play Services that prompts the user to enable location services (if necessary) with just one click.

if your question is at the user level of android these properties are located in: «Settings -> Location -> Use wireless networks» -> «Settings -> Location -> Use GPS satellites» .

Источник

Оцените статью