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
我要求用户许可!查看我的请求中的更新以了解如何。 –