android - 使用 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
}
解决方案
Flutter 电话状态插件会有所帮助!
推荐阅读
- java - 使用 IAM 凭证通过 JDBC 从 Spring 连接到 AWS Aurora 是否合适?
- python - Python中的“尝试相对导入超出顶级包”错误意味着什么?
- python - TypeError:加载 .npy 文件时图像数据无法转换为浮点数
- c++ - 如果无效,如何重复用户输入的电子邮件
- react-native-android - 如何在 react-native 中使用 axios 而不是 fetch?
- c++ - 编译如何选择调用哪个构造函数?
- excel - Oracle - 将结果导出到 Excel,标题/列超过 30 个字符
- javascript - JavaScript中字符串字符之间所有可能的点组合
- reactjs - 如何修复此打字稿承诺错误?
- android - ItemTouchHelper.callback 中的 ConvertToAbsoluteDirection()