0
我使用此代碼獲取當前位置並且工作正常。但在商店發佈後,它不再適用。應用程序保持鎖定在doInBackground函數中。Android:在Play商店發佈後無法獲取當前位置
private class GpsAsync extends AsyncTask<Void, Integer, Boolean> {
private ContactLocationListener contactLocationListener;
private Location location;
@Override
protected void onPreExecute() {
super.onPreExecute();
contactLocationListener = new ContactLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, contactLocationListener);
}
@Override
protected Boolean doInBackground(Void... voids) {
while (location == null) {}
return null;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
textViewLatitude.setText(String.valueOf(location.getLatitude()));
textViewLongitude.setText(String.valueOf(location.getLongitude()));
}
public class ContactLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
location = loc;
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {}
@Override
public void onProviderEnabled(String s) {}
@Override
public void onProviderDisabled(String s) {}
}
}
我的清單:
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 50);
} else {
// start GpsAsync
}
謝謝爲你的答案。 - GPS已打開(我可以在Google地圖上獲取位置) - 在調用GpsAsync之前檢查權限 - 對於應用程序,我們需要一個精確的位置,然後我們使用GPS提供商 –
再次,從API 23中檢查代碼中的權限是不夠的。用戶必須明確同意或拒絕使用某種服務即位置)。 我會在這樣的彈出窗口的答案示例中更新。 – Cadet
我要求用戶許可!查看我的請求中的更新以瞭解如何。 –