SHARE
TWEET

Untitled

a guest Jan 24th, 2020 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Receiver
  2. class  ReceiveSMS : BroadcastReceiver() {
  3.  
  4.     val TAG = "ReceiveSMS"
  5.     private var bundle: Bundle? = null
  6.     private var currentSMS: SmsMessage? = null
  7.     private var message: String? = null
  8.  
  9.     override fun onReceive(context: Context?, intent: Intent?) {
  10.         if (intent!!.action!!.equals("android.provider.Telephony.SMS_RECEIVED")) {
  11.             bundle = intent.extras
  12.             if (bundle != null) {
  13.                 val pduObjects = bundle!!.get("pdus") as Array<*>
  14.                 for (aObject in pduObjects) {
  15.                     currentSMS = getIncomingMessage(aObject!!, bundle!!)
  16.                     val senderNo: String = currentSMS!!.displayOriginatingAddress
  17.                     message = currentSMS!!.displayMessageBody
  18.                     Toast.makeText(context, "senderNum: $senderNo :\n message: $message", Toast.LENGTH_LONG).show()
  19.                 }
  20.                 abortBroadcast()
  21.             }
  22.         }
  23.     }
  24.  
  25.     private fun getIncomingMessage(aObject: Any, bundle: Bundle): SmsMessage? {
  26.         val currentSMS: SmsMessage
  27.         currentSMS = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  28.             val format = bundle.getString("format")
  29.             SmsMessage.createFromPdu(aObject as ByteArray, format)
  30.         } else {
  31.             SmsMessage.createFromPdu(aObject as ByteArray)
  32.         }
  33.         return currentSMS
  34.     }
  35.  
  36.   /*  fun checkCode(msgFrom: String, msgBody: String) {
  37.  
  38.     }*/
  39. }
  40.  
  41.  
  42. //Activity отрывки
  43.   fun replaceFragment(tag: String, fragment: Fragment) {
  44.         val intentFilter = IntentFilter("android.provider.Telephony.SMS_RECEIVED")
  45.         registerReceiver(receiveSMS, intentFilter)
  46.  
  47.         fragmentManager.beginTransaction()
  48.                 .replace(R.id.fragment_container, fragment, tag).commit()
  49.     }
  50.  
  51.   override fun onDestroy() {
  52.         super.onDestroy()
  53.         try {
  54.             unregisterReceiver(receiveSMS)
  55.         } catch(exc: IllegalArgumentException) {
  56.             Log.i(TAG, "Receiver not registered.")
  57.         }
  58.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top