How to prompt user to enable GPS_PROVIDER and/or NETWORK_PROVIDER?

前端 未结 4 1946
死守一世寂寞
死守一世寂寞 2020-12-05 17:54

I need to get location of user. I put in manifest

相关标签:
4条回答
  • 2020-12-05 18:00

    Implement LocationListner : so

     @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();
    }
    }     
    

    Show dialog to open Settings:

    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();
    }
    

    In your OnCreate Method call :

     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);
    

    And ovveride onActivityResult method to test if the user activated correctly 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);
    }
     }
    
    0 讨论(0)
  • 2020-12-05 18:03

    LOCATION_MODE will be LOCATION_MODE_OFF if GPS is off, which will return value 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);
        }
    
    0 讨论(0)
  • 2020-12-05 18:05

    you can use

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

    after checking gps.

    complete source can be found HERE or you can refer to SO ANSWER

    0 讨论(0)
  • 2020-12-05 18:18

    You can start an options intent for the location settings.

    Intent gpsOptionsIntent = new Intent(  
        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);  
    startActivity(gpsOptionsIntent);
    
    0 讨论(0)
提交回复
热议问题