onProviderDisabled对话框未显示

时间:2014-04-25 16:43:53

标签: android google-maps locationmanager

我刚刚开始使用谷歌地图,我正试图在GPS关闭时出现一个对话框。 这是我的onProviderDisabled方法的代码。

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

}

我还将此添加到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);
        }



    }

感谢您的期待。 : - )

2 个答案:

答案 0 :(得分:2)

我在OnCreate中使用了类似的东西:

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

功能代码

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

答案 1 :(得分:0)

如果有人知道,我通过添加一个名为isGPSEnabled的布尔值并让它来检查GPS的状态来实现这一点。然后我使用if和else来调用onProviderDisabled方法 OnCreate方法如下。

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