Skip to content

Instantly share code, notes, and snippets.

@dilipsuthar97
Created June 12, 2019 06:58
Show Gist options
  • Save dilipsuthar97/387eb16a3dd5ea17d70948ce1f59f542 to your computer and use it in GitHub Desktop.
Save dilipsuthar97/387eb16a3dd5ea17d70948ce1f59f542 to your computer and use it in GitHub Desktop.
class MainActivity : AppCompatActivity() {
companion object {
const val RC_CODE = 101
const val TAG = "debug_MainActivity"
const val SENT = "SMS_SENT"
const val DELIVERED = "SMS_DELIVERED"
}
// Views
@BindView(R.id.toolbar) lateinit var toolbar: Toolbar
@BindView(R.id.btnSendSMS) lateinit var sendSMS: FloatingActionButton
@BindView(R.id.fieldNumber) lateinit var fieldNumber: TextInputEditText
@BindView(R.id.fieldMessage) lateinit var fieldMessage: TextInputEditText
private lateinit var sentPI: PendingIntent
private lateinit var deliveredPI: PendingIntent
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
ButterKnife.bind(this)
// Receiver listener for SMS
sentPI = PendingIntent.getBroadcast(this, 0, Intent(SENT), 0)
deliveredPI = PendingIntent.getBroadcast(this, 0, Intent(DELIVERED), 0)
// --when the sms has been sent
this.registerReceiver(object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
when (resultCode) {
Activity.RESULT_OK ->
Toasty.success(this@MainActivity, "SMS sent success!", Toasty.LENGTH_SHORT, true).show()
SmsManager.RESULT_ERROR_NO_SERVICE ->
Toasty.error(this@MainActivity, "No active network to send SMS.", Toasty.LENGTH_SHORT, true).show()
SmsManager.RESULT_ERROR_RADIO_OFF ->
Toasty.error(this@MainActivity, "SMS not sent!", Toasty.LENGTH_SHORT, true).show()
}
}
}, IntentFilter(SENT))
// --When SMS has been delivered
this.registerReceiver(object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
when (resultCode) {
Activity.RESULT_OK ->
Toasty.success(this@MainActivity, "SMS delivered.", Toasty.LENGTH_SHORT, true).show()
Activity.RESULT_CANCELED ->
Toasty.error(this@MainActivity, "SMS not delivered.", Toasty.LENGTH_SHORT, true).show()
}
}
}, IntentFilter(DELIVERED))
initToolbar()
initComponent()
}
private fun initToolbar() {
setSupportActionBar(toolbar)
supportActionBar?.title = resources.getString(R.string.app_name)
}
private fun initComponent() {
// Action send SMS
sendSMS.setOnClickListener {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED) {
val message = fieldMessage.text.toString()
val number = fieldNumber.text.toString()
if (message.isEmpty() || number.isEmpty())
Toasty.error(this@MainActivity, "Please enter number and message!", Toasty.LENGTH_SHORT, true).show()
else {
try {
val smsManager = SmsManager.getDefault()
smsManager.sendTextMessage(number, null, message, sentPI, deliveredPI)
} catch (e: Exception) {
Log.d(TAG, e.message)
e.printStackTrace()
Toasty.error(this@MainActivity, "SMS Failed to send, please try again!", Toasty.LENGTH_SHORT, true).show()
}
}
} else
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.SEND_SMS), RC_CODE)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment