Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Kotlin - how to detect and read received SMS

I know there are a lot of questions like this already floating around, and I've tried all that I could come across, but I still can't get it to work.

My problem is the BroadcastReceiver onReceive never seem to be called. My code is as follows:

class SMSReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
    Log.d("BroadcastReceiver", "onReceive")
    if (intent.action == Telephony.Sms.Intents.SMS_RECEIVED_ACTION) {
        Log.d("BroadcastReceiver", "SMS received")
        // Will do stuff with message here
    }
}

The log messages never show up.

AndroidManifest.xml

<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar">
    <activity android:name=".main.MainActivity" 
    android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" 
            />
        </intent-filter>
    </activity>
    <activity android:name=".setup.SetupActivity" 
     android:screenOrientation="portrait">
    </activity>
    <receiver
            android:name=".SMSReceiver"
            android:enabled="true"
            android:exported="true">
        <intent-filter android:priority="1000">
            <action 
         android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
</application>

In my mainActivity I've tried multiple ways of achieving this, but currently there's only:

var smsReceiver = SMSReceiver()

I appreciate any tips I can get, and it would also be great if any code samples was written in Kotlin. :)

like image 286
Anders Avatar asked Mar 03 '19 18:03

Anders


People also ask

How do I check SMS permission on Android?

To set the app's permission on a device or emulator instance, choose Settings > Apps > SMS Messaging > Permissions, and turn on the SMS permission for the app.


1 Answers

I got into the same issue actually it was only permissions issue even if I was requesting all permissions and allowing all at once still some permissions were not grated, make sure by applying debugging tags that you have all permissions allowed.

I used the following

private val appPermission = arrayOf(Manifest.permission.READ_SMS, Manifest.permission.RECEIVE_MMS)

in onCreate

// Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.RECEIVE_SMS)
            != PackageManager.PERMISSION_GRANTED) {

        // Permission is not granted
        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.RECEIVE_SMS)) {
            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.
        } else {
            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(this,
                    arrayOf(Manifest.permission.RECEIVE_SMS),
                    PERMISSIONS_RECEIVE_SMS)

            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    } else {
        // Permission has already been granted
    }

here is onRequestPermission

override fun onRequestPermissionsResult(requestCode: Int,
                                            permissions: Array<String>, grantResults: IntArray) {
        when (requestCode) {
            PERMISSIONS_RECEIVE_SMS -> {
                // If request is cancelled, the result arrays are empty.
                if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.
                    Log.d(TAG, "PERMISSIONS_RECEIVE_SMS permission granted")

                    // Here, thisActivity is the current activity
                    if (ContextCompat.checkSelfPermission(this,
                                    Manifest.permission.READ_SMS)
                            != PackageManager.PERMISSION_GRANTED) {

                        // Permission is not granted
                        // Should we show an explanation?
                        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                                        Manifest.permission.READ_SMS)) {
                            // Show an explanation to the user *asynchronously* -- don't block
                            // this thread waiting for the user's response! After the user
                            // sees the explanation, try again to request the permission.
                        } else {
                            // No explanation needed, we can request the permission.
                            ActivityCompat.requestPermissions(this,
                                    arrayOf(Manifest.permission.READ_SMS),
                                    PERMISSIONS_REQUEST_READ_SMS)

                            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
                            // app-defined int constant. The callback method gets the
                            // result of the request.
                        }
                    } else {
                        // Permission has already been granted
                    }


                } else {
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    Log.d(TAG, "PERMISSIONS_RECEIVE_SMS permission denied")
                }
                return
            }

            PERMISSIONS_REQUEST_READ_SMS -> {
                // If request is cancelled, the result arrays are empty.
                if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.
                    Log.d(TAG, "PERMISSIONS_REQUEST_READ_SMS permission granted")
                } else {
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                    Log.d(TAG, "PERMISSIONS_REQUEST_READ_SMS permission denied")
                }
                return
            }

            // Add other 'when' lines to check for other
            // permissions this app might request.
            else -> {
                // Ignore all other requests.
            }
        }
    }
like image 158
user473445 Avatar answered Nov 15 '22 10:11

user473445