locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
//getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//getting network status
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
兩者都是假的,雖然我沒有檢查棉花糖,還有像運行權限,所以我也跟着這些鏈接link1和link2Android的棉花糖,locationManager.isProviderEnbled總是返回false
,做一些調試和推杆checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION);
isNetworkEnabled之前返回0,這是PERMISSION_GRANTED
代碼
SplashScreenActivity
public class SplashScreenActivity extends Activity {
private static final String TAG = "SplashScreenActivity";
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_splashscreen);
int hasLocationPermission = checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
if (hasLocationPermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_CODE_ASK_PERMISSIONS);
return;
}
//comes till here . ie hasLocationPermission is true
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION); //breakpoint at this line gives 0 ie PERMISSION_GRANTED
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); //false at debug breakpoint
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);//false at debug breakpoint
}
}
清單Here
你在哪裏執行此操作?在模擬器中還是在真實的設備上?如果是真的,那麼請說明:Model,Build(操作系統版本和內部版本號)以及任何可能相關的logcat ....最有可能看起來你有兩個不同的活動或發信人,1有權限,1沒有。 ..也'hasWriteContactsPermission'檢查GPS是不好的做法...使用其他人可能讀取和理解的名稱正確的是什麼變量....同樣適用於'Build.VERSION.SDK_INT> = 23' [chek this ](http://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad) – Bonatti
請檢查一次。 http://stackoverflow.com/questions/16748300/locationmanager-isproviderenabledlocationmanager-network-provider-is-not-relia?lq=1 –
@Bonatti更新到hasLocationPermission,在設備中執行此操作:Samsung Edge 6,android M,android ver 6.0.1 build#MMB29K.G925IDVU3EPC5,在這裏記錄http://pastebin.com/9WstqwL1,但是通過調試,我能夠指出這個問題是在locationManager.isProviderEnabled只有 –