2017-02-13 69 views
2

我已經尋找解決方案,並嘗試了我可以找到相關的,但仍然無法得到這個工作。代碼的特定部分未運行。我遇到的問題是,這一切都適用於我的舊NEC地形(4.0.1,API 15),但不適用於我的新黑莓Priv(6.0.1,API 23)。在新手機中,第一個錯誤日誌「Net enabled」顯示在logcat中,然後沒有其他錯誤日誌出現(我正在使用它們進行故障排除)。在舊手機中,一切正常,所有日誌都顯示出來。什麼阻止這段代碼運行?我添加了用於添加權限的代碼,我已在下面發佈。我也有我的清單如下:在Android 6.0.1中的位置返回0.0,但在4.0.1中工作

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission-sdk-23 android:name="android.permission.INTERNET" /> 
<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
<uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION"/> 

下面是的LocationManager聲明:

locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE); 

而這裏的代碼:

if (isNetworkEnabled) { 
 
       Log.e("Tag", "Net Enabled"); 
 

 
       if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
 
        Log.e("Tag", "No permission"); 
 
       } 
 
       locationManager.requestLocationUpdates(
 
         LocationManager.NETWORK_PROVIDER, 
 
         MIN_TIME_BW_UPDATES, 
 
         MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
 

 
       if (locationManager == null) { 
 
        Log.e("Tag", "It's null."); 
 
       } 
 
       if (locationManager != null) { 
 
        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
 
        Log.e("Tag", "NetLocMan OK"); 
 

 
        if (location != null) { 
 
         Log.e("Tag", "NetLoc OK"); 
 
         latitude = location.getLatitude(); 
 
         longitude = location.getLongitude(); 
 
         Log.e("Tag", "" + latitude); 
 
         Log.e("Tag", "" + longitude); 
 
        } 
 
       } 
 

 
      } 
 

 
      if (isGPSEnabled) { 
 
       Log.e("Tag", "Location" + location); 
 
       if (location == null) { 
 
        Log.e("Tag", "GPSLoc finding"); 
 
        locationManager.requestLocationUpdates(
 
          LocationManager.GPS_PROVIDER, 
 
          MIN_TIME_BW_UPDATES, 
 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 
 
        Log.e("Tag", "Location" + location); 
 

 
        if (locationManager != null) { 
 
         Log.e("Tag", "GPSLocMan OK"); 
 
         location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
 
         Log.e("Tag", "Location" + location); 
 

 
         if (location != null) { 
 
          latitude = location.getLatitude(); 
 
          longitude = location.getLongitude(); 
 
          Log.e("Tag", "" + latitude); 
 
          Log.e("Tag", "" + longitude); 
 
         } 
 
        } 
 
       } 
 
      }

這是我正在使用的權限代碼:

private static final int PERMS_REQUEST_CODE = 123; 
 

 
if (!hasPermissions()){ 
 
      requestPerms(); 
 
     } 
 

 
private boolean hasPermissions(){ 
 
     int res = 0; 
 
     //string array of permissions, 
 
     String[] permissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}; 
 

 
     for (String perms : permissions){ 
 
      res = checkCallingOrSelfPermission(perms); 
 
      if (!(res == PackageManager.PERMISSION_GRANTED)){ 
 
       return false; 
 
      } 
 
     } 
 
     return true; 
 
    } 
 

 
@Override 
 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
 
     boolean allowed = true; 
 

 
     switch (requestCode){ 
 
      case PERMS_REQUEST_CODE: 
 

 
       for (int res : grantResults){ 
 
        // if user granted all permissions. 
 
        allowed = allowed && (res == PackageManager.PERMISSION_GRANTED); 
 
       } 
 

 
       break; 
 
      default: 
 
       // if user not granted permissions. 
 
       allowed = false; 
 
       break; 
 
     } 
 

 
     if (allowed){ 
 
      //user granted all permissions we can perform our task. 
 

 
     } 
 
     else { 
 
      // we will give warning to user that they haven't granted permissions. 
 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
 
       if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)){ 
 
        Toast.makeText(this, "Permissions denied.\nCannot continue.", Toast.LENGTH_SHORT).show(); 
 
       } 
 
       if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_COARSE_LOCATION)){ 
 
        Toast.makeText(this, "Permissions denied.\nCannot continue.", Toast.LENGTH_SHORT).show(); 
 
       } 
 
      } 
 
     } 
 

 
    }

感謝您的幫助!如果您需要更多代碼,請讓我知道。

+0

這是重複的。現在我知道了,我能做些什麼呢?刪除是否可接受? – swordguy8

+0

嗯,我改變了一些問題,因爲權限不起作用(權限是在應用程序中授予的,但未運行的代碼仍然不會運行)。這在技術上還是重複的? – swordguy8

回答

3

採用Android 6.0(API等級23),谷歌開始要求開發人員在運行時,要求有潛在危險的權限:

ActivityCompat.requestPermissions(MainMenu.this, 
     Manifest.permission.ACCESS_FINE_LOCATION, 
     GET_PERMISSIONS_REQUEST_CODE 
); 

這顯示了一個彈出窗口,詢問是否允許訪問用戶的位置。您之前提出的任何位置請求都將返回空白。

+0

即使我手動授予權限也是如此? – swordguy8

+0

@ swordguy8是的,它的實現是爲了防止應用程序開發人員偷偷地將諸如位置之類的權限放入其應用程序中,而不需要實際「需要」它們。 – Epicality

+0

整潔!謝謝。關閉教程... – swordguy8

相關問題