我嘗試獲取Android設備上用戶的位置。程序檢查GPS的可用性。如果GPSProvider禁用,則警告用戶。如果用戶不啓用GPS,請編程檢查networkProvider。網絡連接已關閉,但位置提供商爲NetworkProvider
代碼檢查網絡提供商,
public boolean askNetworkProvider(){
if(!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
AlertDialog builder = new AlertDialog.Builder(context)
.setMessage("Enable Network Connection")
.setTitle("Warning!")
.setCancelable(true)
.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
return;
}
}).show();
}
if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 0, networkListener);
mlocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.i(LocationService.class.getName(),"Network Enable!!");
return true;
}
return false;
}
即使WiFi和移動連接禁用,它說networkProvider是啓用和if語句進入第二。但它應該是錯誤的。我該如何解決它?
這是真的,謝謝! –