Android 6.0 api 23 - After obtaining permission , my position is null
本问题已经有最佳答案,请猛点这里访问。
我在android 6.0中有位置的另一个问题。
我阅读了Android 6.0权限的官方指南,现在可以正常工作,但我的位置为空,代码似乎被阻止了!
这是OnCreate方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | if ( Build.VERSION.SDK_INT >= 23 ) { //CONTROLLO PER ANDROID 6.0 O SUPERIORE int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION); if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) { } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_ACCESS_FINE_LOCATION); } } }else{ Log.d("ANDROID","ANDROID SENZA FRONZOLI"); //dA QUI IN POI MAIN STANDARD } |
这是检查权限类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_ACCESS_FINE_LOCATION: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { Log.d("PERMESSI-fine","PERMESSI OK"); LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); if ( ContextCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED ) { ActivityCompat.requestPermissions( this, new String[] { android.Manifest.permission.ACCESS_FINE_LOCATION }, MY_PERMISSIONS_ACCESS_FINE_LOCATION ); } Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { double longitude = location.getLongitude(); double latitude = location.getLatitude(); Log.d("GPS-PROVIDER", String.valueOf(longitude)); Log.d("GPS-PROVIDER", String.valueOf(latitude)); // Toast.makeText(MainActivity.this,"GPS OK", Toast.LENGTH_SHORT).show(); }else{ Log.d("GPS","NIENTE ZONA VUOTA"); Log.d("GPS", String.valueOf(location)); } } else { Log.d("PERMESSI-fine","PERMESSE NO"); } return; } // other 'case' lines to check for other // permissions this app might request } } |
代码不给出错误,简单没什么! null对象。 现在我正在使用android 6.0模拟器。 我没有这个操作系统版本的真实设备。
请帮我。
模拟器没有gps,因此位置提供程序根本不会获取任何位置或任何最后已知的位置。您需要添加gps数据。 按照链接或android.developer源