본문 바로가기
반응형

안녕하세요.

 

안드로이드 앱 웹뷰 권한 설정에 대해 알아보겠습니다.

 

안드로이드 앱 개발시 지도를 이용하는 경우와 지도를 이용한 웹뷰를 올리는 경우가 많습니다. 

 

웹 환경에서 GPS 정보를 받아와 현재 위치를 찾을 수 있지만 웹뷰로 올려 안드로이드 앱 개발 시 GPS 정보를 받아오지 못하는 경우에 권한 설정에 대해 알아보겠습니다.

 

 

먼저 웹뷰에서 인터넷을 사용하기 위해 AndroidManifest.xml에 permission을 넣어줍니다.

위치는 <application></application> 위에 넣어주시면 됩니다.

 

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

 

 

그리고 그 아래에 지도권한을 사용하기 위한 permission도 같이 넣어줍니다.

 

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

 

이어서 MainAactivity에 권한 체크와 GPS사용 여부에 대해 설정해줍니다.

 

oncreate에서 위치권한 사용 체크[permissionCheck()]와 GPS 사용 여부[GPRSetting()]를 체크하기 위해 함수를 만들어 줍니다.

 

private void GPSSetting() {
   ContentResolver res = getContentResolver();

   boolean gpsEnabled = Settings.Secure.isLocationProviderEnabled(res, LocationManager.GPS_PROVIDER);
   if(!gpsEnabled) {
      new AlertDialog.Builder(this)
         .setTitle("GPS 설정")
         .setMessage("GPS를 사용하시겠습니까?")
         .setPositiveButton("사용", new DialogInterface.OnClickListener() {
         @Override
            public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent); }
          })
         .setNegativeButton("거절", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
          }).show();
       }
    }
private void permissionCheck(){
   if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
       == PackageManager.PERMISSION_GRANTED){
   } else{
      ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, My_ACCESS_FINE_LOCATION);

   }
}

위에 권한 체크가 안될 경우 아래 코드로 진행하셔도 됩니다.

 


if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
   != PackageManager.PERMISSION_GRANTED) {
   if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION)) {
   } else {
   ActivityCompat.requestPermissions(this, new String[]
   {Manifest.permission.ACCESS_BACKGROUND_LOCATION}, My_ACCESS_FINE_LOCATION);
   }
}

 

반응형