Advertisement
Guest User

Untitled

a guest
Jul 14th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 10.20 KB | None | 0 0
  1. package blackwell.testapp
  2.  
  3. import android.Manifest
  4. import android.support.v7.app.AppCompatActivity
  5. import android.os.Bundle
  6. import android.content.Intent
  7. import android.telephony.SmsManager
  8. import android.content.IntentFilter
  9. import android.app.Activity
  10. import android.app.PendingIntent.getBroadcast
  11. import android.content.BroadcastReceiver
  12. import android.content.Context
  13. import android.content.pm.PackageManager
  14. import android.support.v4.app.ActivityCompat
  15. import android.support.v4.content.ContextCompat.checkSelfPermission
  16. import android.text.method.ScrollingMovementMethod
  17. import android.view.KeyEvent
  18. import android.view.View
  19. import java.text.SimpleDateFormat
  20. import java.util.*
  21. import android.view.inputmethod.EditorInfo
  22. import android.widget.*
  23. import android.widget.TextView.OnEditorActionListener
  24.  
  25.  
  26.  
  27.  
  28. var activityReference: Activity? = null
  29.  
  30. class MainActivity : AppCompatActivity() {
  31.  
  32.     val PERMISSION_ALL = 1
  33.     val MAX_SMS_LENGTH = 160
  34.  
  35.     lateinit var textMessage: EditText
  36.     lateinit var phoneNumber: EditText
  37.     lateinit var messageList: TextView
  38.     lateinit var loadCircle: ProgressBar
  39.  
  40.     override fun onCreate(savedInstanceState: Bundle?) {
  41.         super.onCreate(savedInstanceState)
  42.         setContentView(R.layout.activity_main)
  43.  
  44.         checkAndRequestPermissions()
  45.  
  46.         activityReference = this
  47.  
  48.         messageList = findViewById(R.id.msgList) as TextView
  49.         messageList.movementMethod = ScrollingMovementMethod()
  50.  
  51.         phoneNumber = findViewById(R.id.phone_no) as EditText
  52.         textMessage = findViewById(R.id.msg) as EditText
  53.  
  54.         loadCircle = findViewById(R.id.lBar) as ProgressBar
  55.         loadCircle.setVisibility(View.INVISIBLE);
  56.  
  57.         textMessage.setOnEditorActionListener() { v, actionId, event ->
  58.             if(actionId == EditorInfo.IME_ACTION_SEND){
  59.                 sendSMSRequest()
  60.                 true
  61.             } else false
  62.         }
  63.  
  64.         val sendButton = findViewById(R.id.send_btn).setOnClickListener({
  65.             sendSMSRequest()
  66.         })
  67.     }
  68.  
  69.     private fun sendSMS(phoneNumber: String, message: String) {
  70.         val SENT = "SMS_SENT"
  71.         val DELIVERED = "SMS_DELIVERED"
  72.  
  73.         val sentPI = getBroadcast(this, 0,
  74.                 Intent(SENT), 0)
  75.  
  76.         val deliveredPI = getBroadcast(this, 0,
  77.                 Intent(DELIVERED), 0)
  78.  
  79.         val timeStamp = SimpleDateFormat("h:mm a").format(Calendar.getInstance().getTime())
  80.  
  81.         registerReceiver(object : BroadcastReceiver() {
  82.             override fun onReceive(arg0: Context, arg1: Intent) {
  83.                 when (resultCode) {
  84.                     Activity.RESULT_OK
  85.                         -> Toast.makeText(baseContext, "SMS Delivered",
  86.                             Toast.LENGTH_SHORT).show()
  87.                     else
  88.                         -> { Toast.makeText(baseContext, "SMS Delivery Error",
  89.                                 Toast.LENGTH_SHORT).show()
  90.                              messageList.append("[Message Not Sent]\n")
  91.                     }
  92.  
  93.                 }
  94.                 textMessage.text.clear()
  95.                 loadCircle.setVisibility(View.INVISIBLE)
  96.             }
  97.         }, IntentFilter(DELIVERED))
  98.  
  99.         val sms = SmsManager.getDefault()
  100.  
  101.         if(message.length >= MAX_SMS_LENGTH) {
  102.             val messageList = sms.divideMessage(message)
  103.             sms.sendMultipartTextMessage(phoneNumber, null, messageList, null, null)
  104.         } else {
  105.             sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI)
  106.         }
  107.  
  108.         messageList.append("[You -> $phoneNumber @ $timeStamp] $message\n")
  109.         loadCircle.setVisibility(View.VISIBLE)
  110.         Toast.makeText(baseContext, "Sending...",
  111.                 Toast.LENGTH_SHORT).show()
  112.     }
  113.  
  114.     private fun sendSMSRequest() {
  115.         if (checkAndRequestPermissions()) {
  116.  
  117.             val phoneNo = phoneNumber.text.toString()
  118.             val msg = textMessage.text.toString()
  119.  
  120.             if (!phoneNo.isBlank()) {
  121.                 if (!msg.isBlank()) {
  122.                     sendSMS(phoneNo, msg)
  123.                 } else {
  124.                     textMessage.error = "Cannot leave message blank"
  125.                 }
  126.             } else {
  127.                 phoneNumber.error = "Enter A Phone #"
  128.             }
  129.  
  130.         } else Toast.makeText(baseContext, "SMS Permissions Disabled",
  131.                 Toast.LENGTH_SHORT).show()
  132.     }
  133.  
  134.     private fun checkAndRequestPermissions(): Boolean {
  135.         val listPermissionsNeeded = ArrayList<String>()
  136.  
  137.         if (checkSelfPermission(this,
  138.                 Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
  139.                     listPermissionsNeeded.add(Manifest.permission.SEND_SMS)
  140.         }
  141.         if (checkSelfPermission(this,
  142.                 Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) {
  143.                     listPermissionsNeeded.add(Manifest.permission.RECEIVE_SMS)
  144.         }
  145.  
  146.         if (!listPermissionsNeeded.isEmpty()) {
  147.             ActivityCompat.requestPermissions(this, listPermissionsNeeded.toTypedArray(),
  148.                     PERMISSION_ALL)
  149.             return false
  150.         }
  151.  
  152.         return true
  153.     }
  154.  
  155. }
  156.  
  157. package blackwell.testapp
  158.  
  159. import android.content.BroadcastReceiver
  160. import android.content.Context
  161. import android.content.Intent
  162. import android.os.Build
  163. import android.provider.Telephony
  164. import android.support.annotation.RequiresApi
  165. import android.widget.TextView
  166. import android.widget.Toast
  167. import java.text.SimpleDateFormat
  168. import java.util.*
  169.  
  170. class SMSReceiver : BroadcastReceiver() {
  171.  
  172.     @RequiresApi(Build.VERSION_CODES.KITKAT)
  173.     override fun onReceive(context: Context, intent: Intent) {
  174.         val msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent)
  175.         val sms = msgs[0]
  176.  
  177.         val message = sms.messageBody
  178.         val number = sms.originatingAddress
  179.  
  180.         val msgList = activityReference!!.findViewById(R.id.msgList) as TextView
  181.         val timeStamp = SimpleDateFormat("h:mm a").format(Calendar.getInstance().getTime())
  182.  
  183.         msgList.append("[$number @ $timeStamp] $message\n")
  184.     }
  185. }
  186.  
  187.  
  188. <?xml version="1.0" encoding="utf-8"?>
  189. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  190.     xmlns:app="http://schemas.android.com/apk/res-auto"
  191.     xmlns:tools="http://schemas.android.com/tools"
  192.     android:layout_width="match_parent"
  193.     android:layout_height="match_parent"
  194.     tools:context="blackwell.testapp.MainActivity">
  195.  
  196.     <Button
  197.         android:id="@+id/send_btn"
  198.         style="@android:style/Widget.Holo.Light.Button"
  199.         android:layout_width="146dp"
  200.         android:layout_height="48dp"
  201.         android:layout_marginLeft="16dp"
  202.         android:layout_marginTop="18dp"
  203.         android:background="@android:drawable/btn_default_small"
  204.         android:text="SEND"
  205.         android:textAppearance="@style/TextAppearance.AppCompat.Body1"
  206.         app:layout_constraintLeft_toLeftOf="parent"
  207.         app:layout_constraintTop_toBottomOf="@+id/msgList" />
  208.  
  209.     <EditText
  210.         android:id="@+id/phone_no"
  211.         android:layout_width="259dp"
  212.         android:layout_height="42dp"
  213.         android:layout_marginLeft="16dp"
  214.         android:layout_marginTop="16dp"
  215.         android:background="@android:drawable/editbox_background"
  216.         android:hint="Phone Number"
  217.         android:inputType="phone"
  218.         app:layout_constraintLeft_toLeftOf="parent"
  219.         app:layout_constraintTop_toTopOf="parent" />
  220.  
  221.     <EditText
  222.         android:id="@+id/msg"
  223.         android:layout_width="327dp"
  224.         android:layout_height="142dp"
  225.         android:layout_marginLeft="0dp"
  226.         android:layout_marginTop="18dp"
  227.         android:background="@android:drawable/editbox_background"
  228.         android:gravity="top|left"
  229.         android:hint="Message"
  230.         android:imeOptions="actionSend"
  231.         android:inputType="textMultiLine"
  232.         app:layout_constraintLeft_toLeftOf="@+id/phone_no"
  233.         app:layout_constraintTop_toBottomOf="@+id/phone_no" />
  234.  
  235.     <TextView
  236.         android:id="@+id/msgList"
  237.         android:layout_width="327dp"
  238.         android:layout_height="221dp"
  239.         android:layout_marginTop="18dp"
  240.         android:background="@android:drawable/editbox_background"
  241.         android:cacheColorHint="@android:color/transparent"
  242.         android:clickable="true"
  243.         android:fontFamily="sans-serif-smallcaps"
  244.         android:gravity="top|left"
  245.         android:paddingLeft="10dp"
  246.         android:scrollbars="vertical"
  247.         android:textAppearance="@style/TextAppearance.AppCompat"
  248.         app:layout_constraintLeft_toLeftOf="@+id/phone_no"
  249.         app:layout_constraintTop_toBottomOf="@+id/msg" />
  250.  
  251.     <ProgressBar
  252.         android:id="@+id/lBar"
  253.         style="@android:style/Widget.Holo.Light.ProgressBar.Inverse"
  254.         android:layout_width="wrap_content"
  255.         android:layout_height="wrap_content"
  256.         android:layout_marginBottom="12dp"
  257.         android:layout_marginLeft="11dp"
  258.         app:layout_constraintBottom_toTopOf="@+id/msg"
  259.         app:layout_constraintLeft_toRightOf="@+id/phone_no" />
  260.  
  261. </android.support.constraint.ConstraintLayout>
  262.  
  263. <?xml version="1.0" encoding="utf-8"?>
  264.  
  265. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  266.     package="blackwell.testapp">
  267.  
  268.     <uses-permission android:name="android.permission.SEND_SMS" />
  269.     <uses-permission android:name="android.permission.RECEIVE_SMS" />
  270.  
  271.     <application
  272.  
  273.         android:allowBackup="true"
  274.         android:icon="@mipmap/ic_launcher"
  275.         android:label="Kotlin SMS Demo"
  276.         android:roundIcon="@mipmap/ic_launcher_round"
  277.         android:supportsRtl="true"
  278.         android:theme="@style/AppTheme">
  279.         <activity android:name=".MainActivity">
  280.             <intent-filter>
  281.                 <action android:name="android.intent.action.MAIN" />
  282.  
  283.                 <category android:name="android.intent.category.LAUNCHER" />
  284.             </intent-filter>
  285.         </activity>
  286.         <receiver android:name="blackwell.testapp.SMSReceiver" >
  287.             <intent-filter>
  288.                 <action android:name="android.provider.Telephony.SMS_RECEIVED" />
  289.             </intent-filter>
  290.         </receiver>
  291.     </application>
  292.  
  293.  
  294. </manifest>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement