public class SplashActivity extends AppCompatActivity { private final String TAG = this.getClass().getSimpleName(); Context mContext;
private final int GPS_ENABLE_REQUEST_CODE = 2001; private final long UPDATE_INTERVAL_MS = 1000 * 60 * 15; private final long FASTEST_UPDATE_INTERVAL_MS = 1000 * 30 ; // 30초 단위로 화면 갱신
private FusedLocationProviderClient mFusedLocationProviderClient; private LocationRequest locationRequest; private double longitude, latitude;
PermissionListener permissionlistener = new PermissionListener() { @Override public void onPermissionGranted() { initView(); }
@Override public void onPermissionDenied(ArrayList<String> deniedPermissions) { Toast.makeText(SplashActivity.this, "권한 허용을 하지 않으면 서비스를 이용할 수 없습니다.", Toast.LENGTH_SHORT).show(); } };
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); mContext = SplashActivity.this; PrefsHelper.init(getApplicationContext()); // 한번만 실행하면 된다.
checkPermissions(); }
private void checkPermissions() {
if(Build.VERSION.SDK_INT >= 26){ // 출처를 알 수 없는 앱 설정 화면 띄우기 PackageManager pm = mContext.getPackageManager(); if (!pm.canRequestPackageInstalls()){ startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, Uri.parse("package:" + getPackageName()))); } }
if (Build.VERSION.SDK_INT >= 23) { // 마시멜로(안드로이드 6.0) 이상 권한 체크 TedPermission.with(mContext) .setPermissionListener(permissionlistener) .setRationaleMessage("앱을 이용하기 위해서는 접근 권한이 필요합니다") .setDeniedMessage("앱에서 요구하는 권한설정이 필요합니다...\n [설정] > [권한] 에서 사용으로 활성화해주세요.") .setPermissions(new String[]{ android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION //android.Manifest.permission.ACCESS_BACKGROUND_LOCATION //앱이 Android 10 이상 앱이 백그라운드에 있는 동안 정기 위치 업데이트를 받을 시 }) .check();
} else { initView(); } }
private void initView() { checkLocationSetting(); }
@SuppressLint("MissingPermission") private void checkLocationSetting() { // https://developer.android.com/training/location/change-location-settings 읽어라. // 위치 요청 설정 locationRequest = LocationRequest.create(); locationRequest.setInterval(UPDATE_INTERVAL_MS); // 위치가 Update 되는 주기 locationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_MS); // 위치 획득후 업데이트되는 주기 locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// 현재 위치 설정 받기 LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest); SettingsClient settingsClient = LocationServices.getSettingsClient(this); settingsClient.checkLocationSettings(builder.build()) .addOnSuccessListener(this, locationSettingsResponse -> { mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(SplashActivity.this); mFusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, null); }) .addOnFailureListener(SplashActivity.this, e -> { int statusCode = ((ApiException) e).getStatusCode(); switch (statusCode) { case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: try { ResolvableApiException rae = (ResolvableApiException) e; rae.startResolutionForResult(SplashActivity.this, GPS_ENABLE_REQUEST_CODE); } catch (IntentSender.SendIntentException sie) { Log.e(TAG, "unable to start resolution for result due to " + sie.getLocalizedMessage()); } break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: Log.e(TAG, "Fix in Settings."); } }); }
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GPS_ENABLE_REQUEST_CODE) { if (resultCode == RESULT_OK) { checkLocationSetting(); } else { finish(); } } }
private LocationCallback locationCallback = new LocationCallback() { @Override public void onLocationResult(LocationResult locationResult) { super.onLocationResult(locationResult); longitude = locationResult.getLastLocation().getLongitude(); latitude = locationResult.getLastLocation().getLatitude(); mFusedLocationProviderClient.removeLocationUpdates(locationCallback);
Log.e(TAG, "latitude:"+latitude+" , longitude:"+longitude); PrefsHelper.write("latitude", String.valueOf(latitude)); PrefsHelper.write("longitude", String.valueOf(longitude));
Intent intent = new Intent(SplashActivity.this, MainActivity.class); startActivity(intent); finish(); }
@Override public void onLocationAvailability(LocationAvailability locationAvailability) { super.onLocationAvailability(locationAvailability); Log.i(TAG, "onLocationAvailability - " + locationAvailability); } };
} |