사용자의 위치를 가져 오는 코드를 작성하여 API < 23에서 올바르게 작동하는 코드를 작성할 수 있습니다.하지만 API 23 이상에서는 Log에서 아무것도 반환하지 않습니다.android API에서 사용자 위치 가져 오기
자세한 내용 :
장치의 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) {
}
} 안드로이드 API에
는'당신 만 가능 업체를 확인하십시오. 이 경우 공급자를 사용할 수 있는지 여부를 확인하고 사용하지 않으면 사용자가 사용할 수 있도록 설정해야합니다. – AndroidHacker
나는 진실을 거짓으로 바꿨지 만 해결하지 못했습니다. 두 번째하는 법? 나는 거의 새로운 안드로이드입니다. –