Reputation:
I have written code for sending and receiving offline SMS using GSM. My sending code is working perfectly but my receiving code is showing an error. Please, someone, check my code and help me identify what I did wrong in my code.
I have tried everything to make my code making enable to receive and show a toast message.
1.MainActivity:
class MainActivity :AppCompatActivity(){
private val requestReceiveSms: Int = 3
private val requestSendSms: Int = 2
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
snd_sms.setOnClickListener {
if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.SEND_SMS) !=
PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.SEND_SMS),
requestSendSms)
}
else{
SendSms()
}
}
if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.RECEIVE_SMS) !=
PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.RECEIVE_SMS),
requestReceiveSms)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,
grantResults: IntArray) {
if(requestCode == requestSendSms)SendSms()
}
private fun SendSms() {
val number = getString(R.string.phone_number)
val text = UserMessage.text.toString()
SmsManager.getDefault().sendTextMessage(number,null,text,null,null)
Toast.makeText(this,"SMS Sent",Toast.LENGTH_SHORT).show()
}
}
2.SmsReceiver Class:
class SmsReceiver : BroadcastReceiver(){
override fun onReceive(context: Context?, intent: Intent) {
val extras = intent.extras
try {
if(extras != null){
var sms: Array<Any> = extras.getString("pdus") as Array<Any>
for(i in sms.indices){
val format = extras.getString("format")
var smsMessage = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
SmsMessage.createFromPdu(sms[i] as ByteArray,format)
}else{
SmsMessage.createFromPdu(sms[i] as ByteArray)
}
var phoneNumber = smsMessage.originatingAddress
val messageText = smsMessage.messageBody.toString()
Toast.makeText(context,"$phoneNumber:(Private)\n" + "messageText: $messageText",Toast.LENGTH_SHORT).show()
}
}
}catch (ex:Exception){
//your error handling code here
//here, consider adding Log.e("SmsReceiver", ex.localizedMessage)
//this log statement simply prints errors to your android studio terminal and will help with debugging, alternatively leave it out
if (context != null){
Toast.makeText(context!!,ex.localizedMessage, Toast.LENGTH_SHORT).show()
}
}
}
}
Actual result: Kotlin typecast Exception : null can not be cast to non-null type kolin.Array
Expected result: Toast message("phone number: private along with message")
Upvotes: 0
Views: 104
Reputation:
enter code here :class SmsReceiverplz: BroadcastReceiver() {
@TargetApi(Build.VERSION_CODES.M)
override fun onReceive(context: Context, intent: Intent) {
val TAG = SmsReceiverplz::class.java.simpleName
val pdu_type = "pdus"
// Get the SMS message.
val bundle = intent.extras
var msgs: Array1<SmsMessage?>
var strMessage = ""
val format = bundle!!.getString("format")
// Retrieve the SMS message received.
val pdus = bundle.get(pdu_type) as Array1<*>?
if (pdus != null) {
// Check the Android version.
val isVersionM = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
// Fill the msgs array.
// msgs = arrayOfNulls1<SmsMessage>(pdus.size)
msgs = arrayOfNulls1<SmsMessage>(size = pdus.size)
for (i in msgs.indices) {
// Check Android version and use appropriate createFromPdu.
if (isVersionM) {
// If Android version M or newer:
msgs[i] = createFromPdu(pdus[i] as ByteArray, format)
} else {
// If Android version L or older:
msgs[i] = createFromPdu(pdus[i] as ByteArray)
}
// Build the message to show.
strMessage += "SMS from " + msgs[i]!!.getOriginatingAddress()
strMessage += " :" + msgs[i]!!.getMessageBody() + "\n"
// Log and display the SMS message.
//Log.d(TAG, "onReceive: $strMessage")
Toast.makeText(context, strMessage, Toast.LENGTH_LONG).show()
}
}
}
}
Upvotes: 0
Reputation: 2729
extras.getString("pdus")
is returning null
and you're telling it to be Array<Any>
which it can't (as it's null)
You need to figure out why extras.getString("pdus")
is returning you null
Have you passed it in properly?
Upvotes: 1