2016-03-28 20 views
0
 locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 

     //getting GPS status 
     isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
     //getting network status 
     isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

兩者都是假的,雖然我沒有檢查棉花糖,還有像運行權限,所以我也跟着這些鏈接link1link2Android的棉花糖,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

+1

你在哪裏執行此操作?在模擬器中還是在真實的設備上?如果是真的,那麼請說明: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

+0

請檢查一次。 http://stackoverflow.com/questions/16748300/locationmanager-isproviderenabledlocationmanager-network-provider-is-not-relia?lq=1 –

+0

@Bonatti更新到hasLocationPermission,在設備中執行此操作:Samsung Edge 6,android M,android ver 6.0.1 build#MMB29K.G925IDVU3EPC5,在這裏記錄http://pastebin.com/9Wstq​​wL1,但是通過調試,我能夠指出這個問題是在locationManager.isProviderEnabled只有 –

回答

1

的問題是與設備,我試着用的Nexus 5X和同一段代碼返回true。 問題是與三星

1

使用最新的棉花糖更新這段代碼,它會在所有的Android工作版本 -

創建一個boolean類型的方法來檢查用戶的權限或不可─

private boolean checkPermission() { 

    boolean flag = true; 

    String[] permissions = {"android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"}; 
    boolean hasPermission = (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED); 
    if (!hasPermission) { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      requestPermissions(permissions, com.jeenees.androidapp.utils.Keys.FILE_READ_PERMISSION); 
      flag = false; 
     } 
    } else { 
     flag = true; 
    } 

    return flag; 
} 

使用您要喜歡 -

LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE); 

if (checkPermission()){ 
      isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); //false at debug breakpoint 
      isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);//false at debug breakpoint 
    } 

希望這將幫助你以上的方法。