2014-04-25 158 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); 
       }