[Android] 문제 해결 부분

728x90

📍 퍼미션 허용 메시지를 키보드가 가리는 문제

🟧 로그인 후 → 키보드 내리기

  • 로그인 처리를 mainActivtiy 위에서 관리되고 있는 LoginFragment 에서 하고 있다.
  • 키보드를 숨기는 코드가 Activity 딴에서만 동작한다.
  • → MainActivity에 키보드 관련 코드를 메소드 단위로 작성한 뒤,
    • LoginFragment에서 로그인 성공 처리 후, Activity로 접근하여 해당 메소드를 호출하였다.
    • [해결 완료!!!]

🟩 MainActivity.kt

fun hideKeyboard() {
    // 키보드 내리기
    val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.hideSoftInputFromWindow(
currentFocus?.windowToken,
        InputMethodManager.HIDE_NOT_ALWAYS
)
}

🟩 LoginFragment.kt

else{ //서버로부터 받은 응답 결과값 = 로그인 성공
activity?.runOnUiThread{
val dialogBuilder = AlertDialog.Builder(requireContext())
        dialogBuilder.setTitle("로그인 성공")
        dialogBuilder.setMessage("로그인 성공하였습니다.")
        dialogBuilder.setPositiveButton("확인"){dialogInterface: DialogInterface, i: Int->
//사용자 정보를 Preferences에 저장 -> 이후 Preference 접근은 액티비티로 접근하면된다.
                                                    //이름 = login_data, 모드 = 이 앱 안에서 데이터 공유 목적
            val pref =activity?.getSharedPreferences("login_data", Context.MODE_PRIVATE)
            val editor = pref?.edit() //편집 사용

            editor?.putInt("login_user_idx", Integer.parseInt(result_text)) //서버로부터 받은 값을 int형변환 후 put 처리
            editor?.commit() //실행

            // 키보드 내리기 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 이 부분 추가 
            val act =activityas MainActivity
            act.hideKeyboard() //호출

            //화면 전환 처리
            val boardMainIntent = Intent(requireContext(),BoardMainActivity::class.java)
            startActivity(boardMainIntent)
activity?.finish()
}
dialogBuilder.show()
}
}

📍 지도 Activity로 화면 전환 시, 구글 본사로 현재 위치를 찍는 문제

  • 지도는 GPS_PROVIDER와 Network_PROVIDER의 LocationManager 중 먼저 사용 가능한 것을 실행
  • 1) Log를 찍어서 위도/경도값을 계속 확인해보았다.
  • 2) GPS는 에뮬레이터 상에 세팅해놓았던 위치로 인식하는데
  • 3) Network는 구글 본사를 현재 위치로 인식한다.

🟧 기존 코드

val location1 = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
val location2 = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)

locationListener =LocationListener{
    getUserLocation(it) // 사용자 위치 정보 가져옴
}

if(location1 != null) {
    Log.d("test_location", "current is location1(GPS): ${location1.latitude}, ${location1.longitude}")
    getUserLocation(location1)
}
if(location2 != null) {
    Log.d("test_location", "current is location2(NETWORK): ${location2.latitude}, ${location2.longitude}")
    getUserLocation(location2)
}

if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER) == true) {
    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0f, locationListener)
}
 if(manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) == true) {
    manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0f, locationListener)
}

🟧 변경된 코드

val location1 = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
val location2 = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)

locationListener =LocationListener{
    getUserLocation(it) // 사용자 위치 정보 가져옴
}

if(location1 != null) {
    Log.d("test_location", "current is location1(GPS): ${location1.latitude}, ${location1.longitude}")
    getUserLocation(location1)
}else if(location2 != null) {
    Log.d("test_location", "current is location2(NETWORK): ${location2.latitude}, ${location2.longitude}")
    getUserLocation(location2)
}

if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER) == true) {
    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0f, locationListener)
} else if(manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER) == true) {
    manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0f, locationListener)
}

▶️ if{ } 문으로 걸러주느냐, if-else{}문으로 걸러주느냐의 차이였을 뿐인데, 큰 차이를 가져왔다.

  • if 문 2개를 사용할 때는 무조건 하나의 조건만 만족하면 건너뛰기 때문에,
  • if - else문에서 2개의 조건을 모두 확인하는 부분이 필요했다.
  • 1) GPS는 현재 내 위치를 제대로 잡고 있기 때문에 먼저 확인하되, 그 위치가 잡히지 않았을 경우 2) 차선책으로 Network로 내 위치를 잡도록 해야 했다.
  • → [이제 현재 위치로 아주 잘 잡는다.]

728x90