Как предложить пользователю включить GPS_PROVIDER и / или NETWORK_PROVIDER? - PullRequest
28 голосов
/ 10 октября 2011

Мне нужно узнать местоположение пользователя.Я вставил в манифест

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Как предложить пользователю включить GPS_PROVIDER и / или NETWORK_PROVIDER?

Ответы [ 4 ]

39 голосов
/ 10 октября 2011

Вы можете начать настройку параметров для настроек местоположения.

Intent gpsOptionsIntent = new Intent(  
    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
startActivity(gpsOptionsIntent);
13 голосов
/ 07 ноября 2015

LOCATION_MODE будет LOCATION_MODE_OFF, если GPS выключен, что вернет значение 0.

int off = Settings.Secure.getInt(getContentResolver(), Settings.Secure.LOCATION_MODE);
    if(off==0){
        Intent onGPS = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(onGPS);
    }
5 голосов
/ 10 октября 2011

вы можете использовать

Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
       startActivity(intent);

после проверки GPS.

полный источник можно найти ЗДЕСЬ или вы можете обратиться к ТАК ОТВЕТ

4 голосов
/ 18 июля 2017

Реализация LocationListner: так

 @Override
 public void onLocationChanged(Location location) {

 }

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

 @Override
 public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider)
{
if (provider.equals(LocationManager.GPS_PROVIDER))
{
    showGPSDiabledDialog();
}
}     

Показать диалог, чтобы открыть Настройки:

public void showGPSDiabledDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("GPS Disabled");
builder.setMessage("Gps is disabled, in order to use the application properly you need to enable GPS of your device");
builder.setPositiveButton("Enable GPS", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), GPS_ENABLE_REQUEST);
    }
}).setNegativeButton("No, Just Exit", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
});
mGPSDialog = builder.create();
mGPSDialog.show();
}

В вашем вызове метода OnCreate:

 LocationManager mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
    return;
}
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

и метод ovveride onActivityResult для проверки правильности активации пользователем gps

    @Override

    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
if (requestCode == GPS_ENABLE_REQUEST)
{
    if (mLocationManager == null)
    {
        mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    }

    if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
    {
        showGPSDiabledDialog();
    }
}
else
{
    super.onActivityResult(requestCode, resultCode, data);
}
 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...