2016-10-08 29 views
4

我可以編寫獲取用戶位置的代碼,並且在API < 23中可以正常工作。但在API 23及更高版本中,我的代碼在Log中沒有返回任何內容。獲取用戶在android API中的位置23

更多詳細信息:
我手動啓用設備的GPS。 在第一次運行應用程序請求權限並且沒有日誌返回。
在接下來的運行應用程序返回我準備的吐司(檢查你的提供者)。

這是我寫的代碼:

public class MainActivity extends AppCompatActivity implements LocationListener { 

private static final int MY_PERMISSIONS_REQUEST_COARSE_LOCATION = 124; 
private static final int MY_PERMISSIONS_REQUEST_FINE_LOCATION = 123; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Here, thisActivity is the current activity 
    if (ContextCompat.checkSelfPermission(this, 
      Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 

     // Should we show an explanation? 
     if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
       Manifest.permission.ACCESS_FINE_LOCATION)) { 

     } else { 

      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_FINE_LOCATION); 
     } 
    } 
    if (ContextCompat.checkSelfPermission(this, 
      Manifest.permission.ACCESS_COARSE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 

     if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
       Manifest.permission.ACCESS_COARSE_LOCATION)) { 

     } else { 
      ActivityCompat.requestPermissions(this, 
        new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 
        MY_PERMISSIONS_REQUEST_COARSE_LOCATION); 

     } 
    } 

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    String provider = locationManager.getBestProvider(new Criteria(), false); 
    Location location = locationManager.getLastKnownLocation(provider); 
    if (location == null) { 
     Toast.makeText(this, "Check your provider", Toast.LENGTH_SHORT).show(); 
    } else { 
     Log.i("New Location", "lat: " + location.getLatitude()); 
     Log.i("New Location", "lng: " + location.getLongitude()); 
    } 
} 

@Override 
public void onLocationChanged(Location location) { 
    Log.i("Location", "lat: " + location.getLatitude()); 
    Log.i("Location", "lng: " + location.getLongitude()); 
} 

@Override 
public void onStatusChanged(String provider, int status, Bundle extras) { 

} 

@Override 
public void onProviderEnabled(String provider) { 

} 

@Override 
public void onProviderDisabled(String provider) { 

} 

}

+0

在您的getBestProvider()方法中,您只檢查啓用的提供程序,如果提供程序未啓用,該位置將始終返回** null **。 在這種情況下,您需要檢查提供程序是否已啓用,如果已禁用,請讓用戶啓用它。 – AndroidHacker

+0

我改爲真,但沒有解決。如何做第二個?我幾乎是新的android。 –

回答

5

在Android的API 23+有處理視爲 「危險的」 權限的新途徑,that's的android.permission.ACCESS_FINE_LOCATIONandroid.permission.ACCESS_COARSE_LOCATION的情況下等等。如果您已經使用android.permission.ACCESS_FINE_LOCATION,則不需要處理android.permission.ACCESS_COARSE_LOCATION

請從官方文檔中查看此鏈接,該文檔向您展示瞭如何在Android API 23+的運行時處理權限。

https://developer.android.com/training/permissions/requesting.html

這其中也可能會幫助你。

https://developer.android.com/guide/topics/location/strategies.html

如何檢查權限:

int permissionCheck = ContextCompat.checkSelfPermission(thisActivity, 
    Manifest.permission.ACCESS_FINE_LOCATION); 

如何請求權限:

if (ContextCompat.checkSelfPermission(thisActivity, 
      Manifest.permission.ACCESS_FINE_LOCATION) 
    != PackageManager.PERMISSION_GRANTED) { 

    // Should we show an explanation? 
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
     Manifest.permission.ACCESS_FINE_LOCATION)) { 

     // Show an expanation to the user *asynchronously* -- don't block 
     // this thread waiting for the user's response! After the user 
     // sees the explanation, try again to request the permission. 

    } else { 

     // No explanation needed, we can request the permission. 

     ActivityCompat.requestPermissions(thisActivity, 
      new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 
      MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); 

     // MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION is an 
     // app-defined int constant. The callback method gets the 
     // result of the request. 
    } 
} 

請注意,即使你在運行時請求權限,你仍然必須保持Android清單中的權限條目如下:

<manifest ... > 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    ... 
    <!-- Needed only if your app targets Android 5.0 (API level 21) or higher. --> 
    <uses-feature android:name="android.hardware.location.gps" /> 
    ... 
</manifest> 

在檢查並授予權限後,您應該查看上面位置策略的鏈接,您可以在其中找到缺少的詳細信息。

基本上你沒有註冊偵聽器來接收位置更新。在onCreate()方法末尾添加此行後,您應該開始在日誌中查看位置更新。

// Register the listener with the Location Manager to receive location updates 
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this); 
+0

沒有解決問題。我認爲問題不在許可中。因爲再次回到Toast。當Toast變成「location == null」時。 –

+0

請檢查更新的答案。你現在應該很好走。 –

+0

謝謝。你的答案解決了問題。但是,它運行加載locationUpdate方法(一種作弊方式)。我在onCreate的位置getter沒有獲取位置(返回我準備的Toast)。它不是一個解決方案嗎?或者我刪除這些行? –