关于Kotlin中的某些代码,我遇到了一个小问题。 我正在尝试在sendSMS函数中建立发送和传送的短信,然后在函数之后使用CURL发送结果,并每30秒调用一次函数以测试与其他手机的网络连接。
但是我有一个小问题:在onReceive操作期间,我的数据总是递归的。
经过大量研究,显然我需要使用一个unregisteredReceiver,但是如果使用它,我的应用程序将崩溃并且短信不会发送。
例如,在下面的代码中,如果我让unregisterReceiver进入我的函数,则我的应用程序将崩溃。
如果我删除了unregisterReceiver,它将正确加载我的函数,但是每隔30秒,它将添加一个“ testsent”:例如2分钟后,我将有4个testent,并且我的curl将执行4次。
您是否知道如何解决我的问题,以清理我的registerreceiver?
private fun sendSMS(phoneNumber: String, message: String, ID: String) {
val SENT = "SMS_SENT"
val DELIVERED = "SMS_DELIVERED"
val sentPI = PendingIntent.getBroadcast(
this, 0,
Intent(SENT), 0
)
val deliveredPI = PendingIntent.getBroadcast(
this, 0,
Intent(DELIVERED), 0
)
//---when the SMS has been sent---
val broadCastReceiver = object : BroadcastReceiver() {
override fun onReceive(arg0: Context, arg1: Intent) {
println("testsent")
when (resultCode) {
Activity.RESULT_OK -> Toast.makeText(
baseContext, "SMS sent",
Toast.LENGTH_SHORT
).show()
SmsManager.RESULT_ERROR_GENERIC_FAILURE -> Toast.makeText(
baseContext, "Generic failure",
Toast.LENGTH_SHORT
).show()
SmsManager.RESULT_ERROR_NO_SERVICE -> Toast.makeText(
baseContext, "No service",
Toast.LENGTH_SHORT
).show()
SmsManager.RESULT_ERROR_NULL_PDU -> Toast.makeText(
baseContext, "Null PDU",
Toast.LENGTH_SHORT
).show()
SmsManager.RESULT_ERROR_RADIO_OFF -> Toast.makeText(
baseContext, "Radio off",
Toast.LENGTH_SHORT
).show()
}
}
}
registerReceiver(broadCastReceiver, IntentFilter(SENT))
unregisterReceiver(broadCastReceiver)
val url2 = URL("urltosendcUrl")
var reqParam2 = URLEncoder.encode("sms sent", "UTF-8") + "=" + URLEncoder.encode(Activity.RESULT_OK.toString(), "UTF-8")
reqParam2 += '&' + URLEncoder.encode("cdr_id", "UTF-8") + "=" + URLEncoder.encode(ID, "UTF-8")
with(url2.openConnection() as HttpURLConnection) {
requestMethod = "POST" // optional default is GET
val test = OutputStreamWriter(getOutputStream())
test.write(reqParam2)
test.flush()
println("\nSent 'POST2' request to URL2 : $url2; Response Code : $responseCode")
}
//---when the SMS has been delivered---
val broadCastReceiver2 = object : BroadcastReceiver() {
override fun onReceive(arg0: Context, arg1: Intent) {
println("testdelivered")
when (resultCode) {
Activity.RESULT_OK -> Toast.makeText(
baseContext, "SMS delivered",
Toast.LENGTH_SHORT
).show()
Activity.RESULT_CANCELED -> Toast.makeText(
baseContext, "SMS not delivered",
Toast.LENGTH_SHORT
).show()
}
}
}
var reqParam3 = URLEncoder.encode("sms delivered", "UTF-8") + "=" + URLEncoder.encode(Activity.RESULT_OK.toString(), "UTF-8")
reqParam3 += '&' + URLEncoder.encode("cdr_id", "UTF-8") + "=" + URLEncoder.encode(ID, "UTF-8")
with(url2.openConnection() as HttpURLConnection) {
requestMethod = "POST" // optional default is GET
val test = OutputStreamWriter(getOutputStream())
test.write(reqParam3)
test.flush()
println("\nSent 'POST2' request to URL2 : $url2; Response Code : $responseCode")
}
registerReceiver(broadCastReceiver2, IntentFilter(DELIVERED))
unregisterReceiver(broadCastReceiver2)
val sms = SmsManager.getDefault()
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI)
println("testdetest")
}
我只需要每30秒调用一次curl,而不是每增加1次调用就添加一次...