首页 > 解决方案 > 使用 MethodChanneel 检测 Flutter 上的电话

问题描述

我正在尝试使用颤振构建电话拨号器应用程序。我尝试使用 aCallReceiver但它不适用于我,我不知道为什么。一开始我试图实现它只是为了展示一个烤面包机

class CallReceiver:BroadcastReceiver() {


    private lateinit var phoneState: String

    override fun onReceive(context: Context?, intent: Intent?) {

        if (intent!!.getStringExtra(TelephonyManager.EXTRA_STATE)
                ==TelephonyManager.EXTRA_STATE_OFFHOOK
        ){
            phoneState = "phone Call Stated"
            showToastMsg(context!!, msg ="Phone Call is Started....")
        }else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE)
                ==TelephonyManager.EXTRA_STATE_IDLE
        ){showToastMsg(context!!, msg ="Phone Call is Ended....")
            phoneState = "phone Call Ended"
        }
        else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE)
                ==TelephonyManager.EXTRA_STATE_RINGING
        ){showToastMsg(context!!, msg="Phone Call is Ringing....")
            phoneState = "phone Call Ringing"
        } else {
            phoneState = "Nothing Detected"
        }

    }


    fun showToastMsg(c:Context,msg:String){
        val toast = Toast.makeText(c,msg, Toast.LENGTH_LONG)
        toast.setGravity(Gravity.CENTER,0,0)
        toast.show()
    }

}

试图使用它,methodChannel但我被困在如何无意识地调用它

fun onReceive() :String{

        if (intent!!.getStringExtra(TelephonyManager.EXTRA_STATE)
                ==TelephonyManager.EXTRA_STATE_OFFHOOK
        ){
            phoneState = "phone Call Stated"
            showToastMsg(context!!, msg ="Phone Call is Started....")
        }else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE)
                ==TelephonyManager.EXTRA_STATE_IDLE
        ){showToastMsg(context!!, msg ="Phone Call is Ended....")
            phoneState = "phone Call Ended"
        }
        else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE)
                ==TelephonyManager.EXTRA_STATE_RINGING
        ){showToastMsg(context!!, msg="Phone Call is Ringing....")
            phoneState = "phone Call Ringing"
        } else {
            phoneState = "Nothing Detected"
        }
          return phoneState
    }

标签: androidflutterkotlindart

解决方案


Flutter 电话状态插件会有所帮助!


推荐阅读