-
Notifications
You must be signed in to change notification settings - Fork 0
[Fix] NetworkCallback의 onAvailable, onLost가 메인 스레드에서 실행되지 않아 발생하던 문제 수정 #433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,8 @@ import android.net.ConnectivityManager | |
| import android.net.Network | ||
| import android.net.NetworkCapabilities | ||
| import android.net.NetworkRequest | ||
| import android.os.Handler | ||
| import android.os.Looper | ||
| import android.provider.Settings | ||
| import com.eatssu.android.presentation.util.showDialog | ||
|
|
||
|
|
@@ -21,6 +23,9 @@ class NetworkConnection(private val context: Context) : | |
| .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) // 와이파이 사용 관련 감지 | ||
| .build() | ||
|
|
||
| // NetworkCallback은 백그라운드 스레드에서 호출되므로 Dialog 등 UI 작업은 메인 스레드에서 실행해야 함 | ||
| private val mainHandler = Handler(Looper.getMainLooper()) | ||
|
|
||
| // 네트워크 연결 안 되어있을 때 보여줄 다이얼로그 | ||
| private val dialog: Dialog by lazy { | ||
| context.showDialog("네트워크 연결 안 됨", "Wi-Fi, 모바일 데이터를 확인해주세요") { | ||
|
|
@@ -59,18 +64,23 @@ class NetworkConnection(private val context: Context) : | |
| override fun onAvailable(network: Network) { | ||
| super.onAvailable(network) | ||
|
|
||
| if (getConnectivityStatus() == null) { | ||
| // 네트워크 연결 안 되어 있을 때 | ||
| dialog.show() | ||
| } else { | ||
| // 네트워크 연결 되어 있을 때 | ||
| dialog.dismiss() | ||
| mainHandler.post { | ||
| if (getConnectivityStatus() == null) { | ||
| // 네트워크 연결 안 되어 있을 때 | ||
| dialog.show() | ||
| } else { | ||
| // 네트워크 연결 되어 있을 때 | ||
| dialog.dismiss() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 네트워크 끊겼을 때 실행되는 메소드 | ||
| override fun onLost(network: Network) { | ||
| super.onLost(network) | ||
| dialog.show() | ||
|
|
||
| mainHandler.post { | ||
| dialog.show() | ||
| } | ||
|
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lifecycleScope.launch {
if (getConnectivityStatus() == null) {
dialog.show()
} else {
dialog.dismiss()
}
}
아마 이렇게 하면 코루틴 사용해서 생명주기 부분이 더 안정성이 잇는 것으로 압니다..!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오우 그러네요! 제미니 반영까지 해서 올리겠습니당