package Ġcom . ser gio lope z . mobile weather . framework . network Ċ Ċ import Ġandroid . app . Application Ċ import Ġandroid . content . Context Ċ import Ġandroid . net . Connectivity Manager Ċ import Ġandroid . net . Network Ċ import Ġandroid . net . Network Request Ċ import Ġandroidx . annotation . Requires Permission Ċ Ċ class ĠNetwork Monitor Ċ @ Requires Permission ( android . Manifest . permission . ACCESS _ NETWORK _ STATE ) Ċ constructor ( private Ġval Ġapplication : ĠApplication ) Ġ{ ĊĊĠĠĠ Ġfun Ġstart Network Callback () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġconnectivity Manager : ĠConnect ivity Manager Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ Ġapplication . getSystem Service ( Context . CONNECT IVITY _ SERVICE ) Ġas ĠConnect ivity Manager ĊĠĠĠĠĠĠĠ Ġval Ġbuilder : ĠNetwork Request . Builder Ġ= ĠNetwork Request . Builder () ĊĊĠĠĠĠĠĠĠ Ġ/** Check Ġif Ġversion Ġcode Ġis Ġgreater Ġthan ĠAPI Ġ 2 4 */ ĊĠĠĠĠĠĠĠ Ġif Ġ( android . os . Build . VERSION . SDK _ INT Ġ>= Ġandroid . os . Build . VERSION _ CODES . N ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġconnectivity Manager . register Default Network Callback ( network Callback ) ĊĠĠĠĠĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġconnectivity Manager . register Network Callback ( ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġbuilder . build (), Ġnetwork Callback ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġfun Ġstop Network Callback () Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġconnectivity Manager : ĠConnect ivity Manager Ġ= ĊĠĠĠĠĠĠĠĠĠĠĠ Ġapplication . getSystem Service ( Context . CONNECT IVITY _ SERVICE ) Ġas ĠConnect ivity Manager ĊĠĠĠĠĠĠĠ Ġconnectivity Manager . unregister Network Callback ( Connectivity Manager . Network Callback ()) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġval Ġnetwork Callback Ġ= Ġobject Ġ: ĠConnect ivity Manager . Network Callback () Ġ{ ĊĊĠĠĠĠĠĠĠ Ġoverride Ġfun Ġon Available ( network : ĠNetwork ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠNetwork Validator . is Network Connected Ġ= Ġtrue ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġoverride Ġfun Ġon Lost ( network : ĠNetwork ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ ĠNetwork Validator . is Network Connected Ġ= Ġfalse ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠ Ġ} Ċ }