Skip to content

Instantly share code, notes, and snippets.

@TobiAlbert
Last active October 14, 2019 07:55
Show Gist options
  • Select an option

  • Save TobiAlbert/726b2d2e4c73de413a6706136f04e602 to your computer and use it in GitHub Desktop.

Select an option

Save TobiAlbert/726b2d2e4c73de413a6706136f04e602 to your computer and use it in GitHub Desktop.
Makes a request to network provider to get available airtime balance.
private val telephonyManager by lazy {
context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager }
// requires Android v26
// requries CALL_PHONE permission
private fun requestAirtimeBalance(){
when (telephonyManager.simState) {
Telephony.SIM_STATE_READY -> dialUssdCode()
else -> Unit
}
}
private fun dialUssdCode() {
val ussdCode = "*556#" // for MTN lines
val callback = object : TelephonyManager.UssdResponseCallback() {
override fun onReceivedUssdResponse(
telephonyManager: TelephonyManager?,
request: String?,
response: CharSequence?){
super.onReceivedUssdResponse(telephonyManager, request, response)
// Log response data
Log.i("MainActivity", "USSD response: $response")
}
override fun onReceivedUssdResponseFailed(
telephonyManager: TelephonyManager?,
request: String?,
response: CharSequence?){
super.onReceivedUssdFailed(telephonyManager, request, failure)
// Log response data
Log.i("MainActivity", "USSD response failed: $failure")
}
}
telephonyManager.sendUssdRequest(ussdCode, callback, Handler())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment