Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- WebView
- RecyclerView
- Kotlin
- build
- error
- image
- Android
- 안스
- 깃헙
- coroutine
- studio
- 레트로핏
- 안드로이드 스튜디오
- Retrofit
- viewpager
- 웹뷰
- MVVM
- 안드로이드
- 코루틴
- dart
- 코틀린
- 유튜브
- 스튜디오
- 의존성주입
- Gradle
- 에러
- GIT
- 안드로이드스튜디오
- Github
- ADB
Archives
- Today
- Total
코딩하는 일용직 노동자
위치권한을 요청하고, GPS 기능까지 켜도록 하는 예제 본문
위치권한을 요청하고, GPS 기능까지 켜도록 하는 예제입니다.
화면의 버튼을 누르면 우선 위치권한을 확인하고 없다면 요청합니다.
사용자가 거부를 누른다면 환경설정에서 위치권한을 바꿀 수 있도록 팝업을 보여줍니다.
위치권한이 허용 상태일때 GPS 기능이 비활성된 상태라면 다시 팝업을 보여주고, 사용자가 OK를 누르면 GPS 기능을 활성화 시킵니다.
전체소스를 아래에 공개합니다.
package com.test.locationcheckex;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentSender;
import android.content.pm.PackageManager;
import android.location.Location;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import com.google.android.gms.common.api.ResolvableApiException;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResponse;
import com.google.android.gms.location.SettingsClient;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_PERMISSIONS_REQUEST_CODE = 1981;
private static final int REQUEST_CODE_LOCATION_SETTINGS = 2981;
private static final String[] PERMISSIONS = new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
/**
* Provides access to the Fused Location Provider API.
*/
private FusedLocationProviderClient mFusedLocationClient;
/**
* Provides access to the Location Settings API.
*/
private SettingsClient mSettingsClient;
/**
* Stores parameters for requests to the FusedLocationProviderApi.
*/
private LocationRequest mLocationRequest;
/**
* Callback for Location events.
*/
private LocationCallback mLocationCallback;
private LocationSettingsRequest mLocationSettingsRequest;
private Location mLastLocation;
private Button btnCheck;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
btnCheck = (Button) findViewById(R.id.btnCheck);
btnCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkLocation();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_LOCATION_SETTINGS) {
switch (resultCode) {
case Activity.RESULT_OK:
Toast.makeText(MainActivity.this, "Result OK", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(MainActivity.this, "Result Cancel", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
private void init() {
if (mFusedLocationClient == null) {
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
}
mSettingsClient = LocationServices.getSettingsClient(this);
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(20 * 1000);
//mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
mLocationSettingsRequest = builder.build();
}
private void checkLocation() {
if (isPermissionGranted()) {
startLocationUpdates();
} else {
requestPermissions();
}
}
private boolean isPermissionGranted() {
for (String permission : PERMISSIONS) {
if (permission.equals(Manifest.permission.ACCESS_BACKGROUND_LOCATION) && Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
continue;
}
final int result = ContextCompat.checkSelfPermission(this, permission);
if (PackageManager.PERMISSION_GRANTED != result) {
return false;
}
}
return true;
}
private void requestPermissions() {
requestPermissions(PERMISSIONS, REQUEST_PERMISSIONS_REQUEST_CODE);
}
private void startLocationUpdates() {
mSettingsClient.checkLocationSettings(mLocationSettingsRequest).addOnSuccessListener(this, new OnSuccessListener<LocationSettingsResponse>() {
@SuppressLint("MissingPermission")
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
}
}).addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ResolvableApiException) {
resolveLocationSettings(e);
} else {
}
}
});
}
public void resolveLocationSettings(Exception exception) {
ResolvableApiException resolvable = (ResolvableApiException) exception;
try {
resolvable.startResolutionForResult(this, REQUEST_CODE_LOCATION_SETTINGS);
} catch (IntentSender.SendIntentException e1) {
e1.printStackTrace();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE) {
if (grantResults.length <= 0) {
// If user interaction was interrupted, the permission request is cancelled and you
// receive empty arrays.
} else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission granted.
startLocationUpdates();
} else {
// Permission denied.
for (String permission:permissions) {
if ("android.permission.ACCESS_FINE_LOCATION".equals(permission)) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("알림");
builder.setMessage("위치 정보 권한이 필요합니다.\n\n[설정]->[권한]에서 '위치' 항목을 사용으로 설정해 주세요.");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int id)
{
Intent intent = new Intent();
intent.setAction(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.fromParts("package", getPackageName(), null));
startActivity(intent);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int id)
{
Toast.makeText(MainActivity.this, "Cancel Click", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
}
}
}
}
'안드로이드' 카테고리의 다른 글
인앱리뷰(InApp Review) 구현하기 (2) | 2022.02.23 |
---|---|
Photo Picker - Android13 (0) | 2022.02.14 |
무선 디버깅(Wireless debugging) 기능 이용방법. (0) | 2022.02.06 |
ADB 명령어로 딥링크 테스트 하는 방법 (0) | 2022.02.03 |
Android Studio 에서 사용하지 않는 리소스를 검색하고 제거하는 방법 (0) | 2021.11.14 |