Created
September 6, 2023 09:53
-
-
Save amoozeshbebin/7eb60a56c9c06a0f709c9c2d77d21b6d to your computer and use it in GitHub Desktop.
SMS Read And Recive / SMS Permission / Kotlin/android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MainActivity.kt: | |
| package com.example.smspermission | |
| import android.Manifest | |
| import android.content.pm.PackageManager | |
| import androidx.appcompat.app.AppCompatActivity | |
| import android.os.Bundle | |
| import android.widget.Button | |
| import android.widget.Toast | |
| import androidx.activity.result.contract.ActivityResultContracts | |
| class MainActivity : AppCompatActivity() { | |
| lateinit var btn:Button | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| init() | |
| } | |
| fun init(){ | |
| bindViews() | |
| btn.setOnClickListener { | |
| if (checkSelfPermission(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED){ | |
| requestPermissionLauncher.launch(Manifest.permission.READ_SMS) | |
| } | |
| if (checkSelfPermission(Manifest.permission.RECEIVE_SMS) == PackageManager.PERMISSION_GRANTED){ | |
| requestPermissionLauncher.launch(Manifest.permission.RECEIVE_SMS) | |
| } | |
| } | |
| } | |
| val requestPermissionLauncher = | |
| registerForActivityResult( | |
| ActivityResultContracts.RequestPermission() | |
| ) { isGranted: Boolean -> | |
| if (isGranted) { | |
| Toast.makeText(this,"permission GRABTED",Toast.LENGTH_LONG).show() | |
| } else { | |
| Toast.makeText(this,"permission deind",Toast.LENGTH_LONG).show() | |
| } | |
| } | |
| fun bindViews(){ | |
| btn = findViewById(R.id.sms_perm_btn) | |
| } | |
| } | |
| _______________________________________________________________ | |
| Create a Brodcast Receiver : | |
| package com.example.smspermission | |
| import android.content.BroadcastReceiver | |
| import android.content.Context | |
| import android.content.Intent | |
| import android.os.Build | |
| import android.os.Bundle | |
| import android.telephony.SmsMessage | |
| import android.widget.Toast | |
| class MySMSReciver : BroadcastReceiver() { | |
| override fun onReceive(context: Context, intent: Intent) { | |
| val bundle = intent.extras as Bundle | |
| val format = bundle.getString("format") | |
| val pdus = bundle.get("pdus") as Array<*> | |
| if (pdus != null) { | |
| var smsMessages = "" | |
| var messages = mutableListOf<SmsMessage>() | |
| var index = 0 | |
| pdus.forEach { | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
| messages.add(SmsMessage.createFromPdu(it as ByteArray?, format)) | |
| } else { | |
| messages.add(SmsMessage.createFromPdu(it as ByteArray?)) | |
| } | |
| smsMessages += "SMS From ${messages[index].originatingAddress} : " + "${messages[index].messageBody}\n" | |
| index++ | |
| } | |
| Toast.makeText(context,smsMessages,Toast.LENGTH_LONG).show() | |
| } | |
| } | |
| } | |
| _________________________________________________________________________________ | |
| android Manifest: | |
| <uses-feature | |
| android:name="android.hardware.telephony" | |
| android:required="false" /> | |
| <uses-permission android:name="android.permission.READ_SMS"/> | |
| <uses-permission android:name="android.permission.RECEIVE_SMS"/> | |
| <uses-permission android:name="android.permission.SEND_SMS"/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment