2

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. enter image description here

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!

Parth Pandya
  • 63
  • 1
  • 5
Hardik Parmar
  • 712
  • 2
  • 13
  • 28

1 Answers1

1

Try to comment the super.onUnavailable() and add custom dialog box.

It will not open system default dialog box and will show your custom dialog box when WiFi host device is not available. Here is the full sample code, hope it will help you.

override fun onUnavailable() {
        //super.onUnavailable()
        Log.e("TAG", "onUnavailable")

val builder = AlertDialog.Builder(this)  
            //set title for alert dialog  
            builder.setTitle("Device not found")  
            //set message for alert dialog  
            builder.setMessage("No device found to connect, Please try again or check the host device.")  
            builder.setIcon(android.R.drawable.ic_dialog_alert)  
  
            //performing positive action  
            builder.setPositiveButton("Try Again"){dialogInterface, which ->  
                Toast.makeText(applicationContext,"clicked Try again",Toast.LENGTH_LONG).show()  
            }  
            //performing cancel action  
            builder.setNeutralButton("Cancel"){dialogInterface , which ->  
                Toast.makeText(applicationContext,"clicked cancel\n operation cancel",Toast.LENGTH_LONG).show()  
            }   
            // Create the AlertDialog  
            val alertDialog: AlertDialog = builder.create()  
            // Set other dialog properties  
            alertDialog.setCancelable(false)  
            alertDialog.show()  

    }
Parth Pandya
  • 63
  • 1
  • 5