implementation 'com.google.android.gms:play-services-location:21.0.0'
private FusedLocationProviderClient fusedLocationProviderClient; boolean locationReceived=false; Handler timeoutHandler; private void getLocation() { timeoutHandler = new Handler(); fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(IntroActivity.this); if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { stopLocationUpdates(); return; } Task<Location> locationTask = fusedLocationProviderClient.getCurrentLocation(PRIORITY_HIGH_ACCURACY, null); locationTask.addOnSuccessListener(location -> { if (location != null) { locationReceived = true; Log.d("myLog", "location.getLatitude() " + location.getLatitude()); Log.d("myLog", "location.getLongitude() " + location.getLongitude()); } else { } stopLocationUpdates(); }); locationTask.addOnFailureListener(e -> { stopLocationUpdates(); }); timeoutHandler.postDelayed(new Runnable() { @Override public void run() { if (!locationReceived) { stopLocationUpdates(); } } }, 10000); // 10 seconds timeout } private void stopLocationUpdates() { timeoutHandler.removeCallbacksAndMessages(null); locationReceived = true; } |
'안드로이드' 카테고리의 다른 글
자바( JAVA) 현재시각이 특정 시각 범위 안에 드는지 체크(영업시간) (1) | 2024.04.12 |
---|---|
카카오내비 길찾기 자바 (0) | 2024.03.28 |
android webview popup (0) | 2024.01.30 |
안드로이드 다른앱 위에 그리기(노티알림) (0) | 2024.01.23 |
WorkerManager android java (0) | 2024.01.17 |