Unable to find SMS permission in android studio

655 Views Asked by At

I am unable to find android.provider.Telephony action in android studio for creating an app that can receive SMS. Almost every article including android developer says that I have to include a intent filter action android.provider.Telephony.SMS_RECEIVE in manifest file. But I figured out that this action is no more supported by android studio. Please help me

2

There are 2 best solutions below

2
On

pls try this

// Add this in manifest
      <uses-permission android:name="android.permission.RECEIVE_SMS" />
      <uses-permission android:name="android.permission.READ_SMS" /> 

     <receiver android:name=".SMSReciver">
                <intent-filter>
                    <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                </intent-filter>
     </receiver>

//Create new class
    public class SMSReciver extends BroadcastReceiver
    {
        private Context mContext;

        @Override
        public void onReceive(Context context, Intent intent)
        {
            mContext = context;
            Bundle myBundle = intent.getExtras();
            SmsMessage[] messages = null;
            String strMessage = "";

            String lMessageBody = "", lMessageFrom = "";

            if (myBundle != null)
            {
                Object[] pdus = (Object[]) myBundle.get("pdus");
                messages = new SmsMessage[pdus.length];

                for (int i = 0; i < messages.length; i++)
                {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

                    lMessageFrom = messages[i].getOriginatingAddress();
                    lMessageBody = messages[i].getMessageBody();
                }
            }
        }
    }
2
On

You need to give the permission to your manifest file,

<uses-permission android:name="android.permission.RECEIVE_SMS"/>

Then you should include the intent-filter to your manifest,You should have taken a class that extends BroadcastReceiver write the name of that class as receiver name.In my case it is SMSReceivcer.

    <receiver android:name=".SMSReceivcer"
        android:exported="true"
        android:permission="android.permission.BROADCAST_SMS">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        </intent-filter>
    </receiver>