1

I saw many tutorials and StackOverflow discussions about the subject but nothing could help. I may be missing something obvious. So, I'm trying to display a toast message when having an incoming sms.

My manifest looks like this : I tried to put highest priority as advised on another trend, but with no effect :

    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <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/AppTheme">
        <receiver android:name="com.araxide.nottogetherthistime.SMSReceiver" android:exported="true">
            <intent-filter android:priority="2147483647">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

This is SMSReceiver class.

public class SMSReceiver extends BroadcastReceiver {

    private final String ACTION_RECEIVE_SMS = "android.provider.Telephony.SMS_RECEIVED";
    @Override
    public void onReceive(Context context, Intent intent) {
           // Toast.makeText(context, "SMS Received!", Toast.LENGTH_LONG).show();
        if (intent.getAction().equals(ACTION_RECEIVE_SMS)) {
            Toast.makeText(context, "SMS Received!", Toast.LENGTH_LONG).show();
        }
}
}

Should I add something in MainActivity ? Based on the examples I saw, I believe it should work this way. Thanks a lot if you have an idea.

SidGabriel
  • 205
  • 1
  • 3
  • 12

1 Answers1

0

Link This seems like a similar question and working solutions are also posted here.

Niveth Saran
  • 130
  • 2
  • 5
  • 1
    Thank you for the link !! If anyone has same problem : - for API < 26 just adding this : android:enabled="true" to the manifest (receiver section) made it works - for API > 26 : you have to ask user permission for sms in your main activity. This tutorial helped me a lot : https://www.youtube.com/watch?v=pke6sMxOsuw – SidGabriel Oct 15 '19 at 18:00