I am trying to get Try Again system popup click event using NetworkCallback
,
Actually, I have WIFI hotspot
& password and I am trying to connect that hotspot
but if there are no any available network then It should show custom logic in 'Try Again' click event. Below is the code I am referring to.
`if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) {
Log.e("TAG", "connection wifi Q")
var ssid = "AndroidShare_2590" //Wifi hotspot
var password = "adr54adhk523" // auto generated password
val wifiNetworkSpecifier = WifiNetworkSpecifier.Builder()
.setSsid(ssid)
.setWpa2Passphrase(password)
.build()
val networkRequest = NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.setNetworkSpecifier(wifiNetworkSpecifier)
.build()
val connectivityManager =
getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
connectivityManager.activeNetwork
connectivityManager.bindProcessToNetwork(connectivityManager.activeNetwork)
val networkCallback: NetworkCallback = object : NetworkCallback() {
override fun onAvailable(network: Network) {
super.onAvailable(network)
connectivityManager.bindProcessToNetwork(network)
CM.setSp(mActivity, CV.KEY_SSID, ssid)
CM.setSp(mActivity, CV.KEY_PASSWORD, password)
CM.setSp(mActivity, CV.KEY_URL, url)
Log.e("TAG", "onAvailable")
//Log.e("TAG", "Now calling download method....");
val mydir = getDir("mydir", MODE_PRIVATE) //Creating an internal dir;
if (mydir.exists() == false) {
mydir.mkdirs()
}
mBinding.connectec.setTag("slave dot")
mBinding.connectec.setImageResource(R.drawable.selected_dot)
connectivityStopAnimation()
val fileWithinMyDir = File(mydir, "myfile")
Log.e("TAG", "Now calling download method....")
// downloadFile("$url/file/0", fileWithinMyDir)
}
override fun onLosing(network: Network, maxMsToLive: Int) {
super.onLosing(network, maxMsToLive)
Log.e("TAG", "onLosing")
}
override fun onLost(network: Network) {
super.onLost(network)
Log.e("TAG", "losing active connection")
}
override fun onUnavailable() {
super.onUnavailable()
Log.e("TAG", "onUnavailable")
}
}
connectivityManager.requestNetwork(networkRequest, networkCallback)
}`
For more information I have attached a screenshot,
This popup is showing when there are no any wifi hotspot
is available and that event we need to customise.
I have tried onLosing, onLost & onUnavailable
method but not getting success,
In onUnavailable
method is call when cancel
button clicked.
What I am expecting, to get try again
button click event.
Thanks in advance!