2014-04-25 210 views
0

我刚开始使用Google地图,我试图在GPS关闭时显示一个对话框。 这是我的onProviderDisabled方法的代码。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

我用在OnCreate是这样的:

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

功能代码

private Boolean gpsEnable(LocationManager locationManager) { 
    return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
} 
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); 
       }