Вопрос

I've just started working with google maps and I'm trying to get a dialog box to appear when the GPS is turned off. Here is my code for the onProviderDisabled method.

   @Override
    public void onProviderDisabled(String provider) {


        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("GPS is disabled");
        builder.setC

ancelable(false);
    builder.setPositiveButton("Enable GPS", new DialogInterface.OnClickListener()
    {

        @Override
        public void onClick(DialogInterface dialog, int which) 
        {
            Intent startGPS = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(startGPS);

        }


    });


    builder.setNegativeButton("Leave GPS off", new DialogInterface.OnClickListener()
    {

        @Override
        public void onClick(DialogInterface dialog, int which) 
        {
            dialog.cancel();

        }


    });

    AlertDialog alert = builder.create();
    alert.show();

}

I've also added this to the OnCreate...

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mapSettings();



        LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);

        String provider = lm.getBestProvider(new Criteria(), true);

        if(provider == null)
        {

            onProviderDisabled(provider);
        }



    }

Thanks for looking. :-)

Это было полезно?

Решение

I use something like this in the OnCreate:

if(!gpsEnable(myLocationManager)){
    // Do something, how to create a AlertDialog or FragmentDialog
}    

Function Code

private Boolean gpsEnable(LocationManager locationManager) {
    return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}

Другие советы

If anyone whats to know, I got this working by adding a Boolean called isGPSEnabled and getting it to check the status of GPS. Then I used and if/else to call the onProviderDisabled method.
The OnCreate method is below.

   @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            mapSettings();


            LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);

             Boolean isGPSEnabled = lm .isProviderEnabled(LocationManager.GPS_PROVIDER);;
             String provider = lm.getBestProvider(new Criteria(), true);


              if(isGPSEnabled == true)
                {


                }
                else
                {



                    onProviderDisabled(provider);
                }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top