Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package blackwell.testapp
- import android.Manifest
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.content.Intent
- import android.telephony.SmsManager
- import android.content.IntentFilter
- import android.app.Activity
- import android.app.PendingIntent.getBroadcast
- import android.content.BroadcastReceiver
- import android.content.Context
- import android.content.pm.PackageManager
- import android.support.v4.app.ActivityCompat
- import android.support.v4.content.ContextCompat.checkSelfPermission
- import android.text.method.ScrollingMovementMethod
- import android.view.KeyEvent
- import android.view.View
- import java.text.SimpleDateFormat
- import java.util.*
- import android.view.inputmethod.EditorInfo
- import android.widget.*
- import android.widget.TextView.OnEditorActionListener
- var activityReference: Activity? = null
- class MainActivity : AppCompatActivity() {
- val PERMISSION_ALL = 1
- val MAX_SMS_LENGTH = 160
- lateinit var textMessage: EditText
- lateinit var phoneNumber: EditText
- lateinit var messageList: TextView
- lateinit var loadCircle: ProgressBar
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- checkAndRequestPermissions()
- activityReference = this
- messageList = findViewById(R.id.msgList) as TextView
- messageList.movementMethod = ScrollingMovementMethod()
- phoneNumber = findViewById(R.id.phone_no) as EditText
- textMessage = findViewById(R.id.msg) as EditText
- loadCircle = findViewById(R.id.lBar) as ProgressBar
- loadCircle.setVisibility(View.INVISIBLE);
- textMessage.setOnEditorActionListener() { v, actionId, event ->
- if(actionId == EditorInfo.IME_ACTION_SEND){
- sendSMSRequest()
- true
- } else false
- }
- val sendButton = findViewById(R.id.send_btn).setOnClickListener({
- sendSMSRequest()
- })
- }
- private fun sendSMS(phoneNumber: String, message: String) {
- val SENT = "SMS_SENT"
- val DELIVERED = "SMS_DELIVERED"
- val sentPI = getBroadcast(this, 0,
- Intent(SENT), 0)
- val deliveredPI = getBroadcast(this, 0,
- Intent(DELIVERED), 0)
- val timeStamp = SimpleDateFormat("h:mm a").format(Calendar.getInstance().getTime())
- registerReceiver(object : BroadcastReceiver() {
- override fun onReceive(arg0: Context, arg1: Intent) {
- when (resultCode) {
- Activity.RESULT_OK
- -> Toast.makeText(baseContext, "SMS Delivered",
- Toast.LENGTH_SHORT).show()
- else
- -> { Toast.makeText(baseContext, "SMS Delivery Error",
- Toast.LENGTH_SHORT).show()
- messageList.append("[Message Not Sent]\n")
- }
- }
- textMessage.text.clear()
- loadCircle.setVisibility(View.INVISIBLE)
- }
- }, IntentFilter(DELIVERED))
- val sms = SmsManager.getDefault()
- if(message.length >= MAX_SMS_LENGTH) {
- val messageList = sms.divideMessage(message)
- sms.sendMultipartTextMessage(phoneNumber, null, messageList, null, null)
- } else {
- sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI)
- }
- messageList.append("[You -> $phoneNumber @ $timeStamp] $message\n")
- loadCircle.setVisibility(View.VISIBLE)
- Toast.makeText(baseContext, "Sending...",
- Toast.LENGTH_SHORT).show()
- }
- private fun sendSMSRequest() {
- if (checkAndRequestPermissions()) {
- val phoneNo = phoneNumber.text.toString()
- val msg = textMessage.text.toString()
- if (!phoneNo.isBlank()) {
- if (!msg.isBlank()) {
- sendSMS(phoneNo, msg)
- } else {
- textMessage.error = "Cannot leave message blank"
- }
- } else {
- phoneNumber.error = "Enter A Phone #"
- }
- } else Toast.makeText(baseContext, "SMS Permissions Disabled",
- Toast.LENGTH_SHORT).show()
- }
- private fun checkAndRequestPermissions(): Boolean {
- val listPermissionsNeeded = ArrayList<String>()
- if (checkSelfPermission(this,
- Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
- listPermissionsNeeded.add(Manifest.permission.SEND_SMS)
- }
- if (checkSelfPermission(this,
- Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) {
- listPermissionsNeeded.add(Manifest.permission.RECEIVE_SMS)
- }
- if (!listPermissionsNeeded.isEmpty()) {
- ActivityCompat.requestPermissions(this, listPermissionsNeeded.toTypedArray(),
- PERMISSION_ALL)
- return false
- }
- return true
- }
- }
- package blackwell.testapp
- import android.content.BroadcastReceiver
- import android.content.Context
- import android.content.Intent
- import android.os.Build
- import android.provider.Telephony
- import android.support.annotation.RequiresApi
- import android.widget.TextView
- import android.widget.Toast
- import java.text.SimpleDateFormat
- import java.util.*
- class SMSReceiver : BroadcastReceiver() {
- @RequiresApi(Build.VERSION_CODES.KITKAT)
- override fun onReceive(context: Context, intent: Intent) {
- val msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent)
- val sms = msgs[0]
- val message = sms.messageBody
- val number = sms.originatingAddress
- val msgList = activityReference!!.findViewById(R.id.msgList) as TextView
- val timeStamp = SimpleDateFormat("h:mm a").format(Calendar.getInstance().getTime())
- msgList.append("[$number @ $timeStamp] $message\n")
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="blackwell.testapp.MainActivity">
- <Button
- android:id="@+id/send_btn"
- style="@android:style/Widget.Holo.Light.Button"
- android:layout_width="146dp"
- android:layout_height="48dp"
- android:layout_marginLeft="16dp"
- android:layout_marginTop="18dp"
- android:background="@android:drawable/btn_default_small"
- android:text="SEND"
- android:textAppearance="@style/TextAppearance.AppCompat.Body1"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/msgList" />
- <EditText
- android:id="@+id/phone_no"
- android:layout_width="259dp"
- android:layout_height="42dp"
- android:layout_marginLeft="16dp"
- android:layout_marginTop="16dp"
- android:background="@android:drawable/editbox_background"
- android:hint="Phone Number"
- android:inputType="phone"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <EditText
- android:id="@+id/msg"
- android:layout_width="327dp"
- android:layout_height="142dp"
- android:layout_marginLeft="0dp"
- android:layout_marginTop="18dp"
- android:background="@android:drawable/editbox_background"
- android:gravity="top|left"
- android:hint="Message"
- android:imeOptions="actionSend"
- android:inputType="textMultiLine"
- app:layout_constraintLeft_toLeftOf="@+id/phone_no"
- app:layout_constraintTop_toBottomOf="@+id/phone_no" />
- <TextView
- android:id="@+id/msgList"
- android:layout_width="327dp"
- android:layout_height="221dp"
- android:layout_marginTop="18dp"
- android:background="@android:drawable/editbox_background"
- android:cacheColorHint="@android:color/transparent"
- android:clickable="true"
- android:fontFamily="sans-serif-smallcaps"
- android:gravity="top|left"
- android:paddingLeft="10dp"
- android:scrollbars="vertical"
- android:textAppearance="@style/TextAppearance.AppCompat"
- app:layout_constraintLeft_toLeftOf="@+id/phone_no"
- app:layout_constraintTop_toBottomOf="@+id/msg" />
- <ProgressBar
- android:id="@+id/lBar"
- style="@android:style/Widget.Holo.Light.ProgressBar.Inverse"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="12dp"
- android:layout_marginLeft="11dp"
- app:layout_constraintBottom_toTopOf="@+id/msg"
- app:layout_constraintLeft_toRightOf="@+id/phone_no" />
- </android.support.constraint.ConstraintLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="blackwell.testapp">
- <uses-permission android:name="android.permission.SEND_SMS" />
- <uses-permission android:name="android.permission.RECEIVE_SMS" />
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="Kotlin SMS Demo"
- android:roundIcon="@mipmap/ic_launcher_round"
- android:supportsRtl="true"
- android:theme="@style/AppTheme">
- <activity android:name=".MainActivity">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <receiver android:name="blackwell.testapp.SMSReceiver" >
- <intent-filter>
- <action android:name="android.provider.Telephony.SMS_RECEIVED" />
- </intent-filter>
- </receiver>
- </application>
- </manifest>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement