1

So, i am trying to write an app which will send SMS messages, receive and see the messages that have been sent. So far i can send a message, but receiving messages is not working on API 26. My next emulator is API 21 and on that version Toast message appears when message is received.

I have successfully managed permissions required for sending and receiving a sms message.

I have read some documentation, but can't quite find what i need. If there is someone who knows what the problem is i would be thankful for help.

public class SmsReceiver extends BroadcastReceiver {

private static final String TAG = "SmsReceiver";

@Override
public void onReceive(Context context, Intent intent) {

    Bundle bundle = intent.getExtras();

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

        for(int i = 0; i < pdus.length; i++) {
            messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
            if(i == 0) {
                message += messages[i].getOriginatingAddress() + ": ";
            }
            message += messages[i].getMessageBody();
            Toast.makeText(context, message, Toast.LENGTH_LONG).show();
        }
    }
}

}

Manifest file

<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_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/AppTheme">
    <receiver android:name="package.name"
        android:exported="true"
        android:permission="android.permission.BROADCAST_SMS">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

I hope this is enough, if needed i can provide other class or manifest file. Thanks in advance

zulu_papa
  • 415
  • 6
  • 12
  • 1
    https://stackoverflow.com/a/51221721/8089931 please check – Vishal Bhut Sep 02 '19 at 12:56
  • Are you sure you've handled the permissions correctly? Are you explicitly requesting `RECEIVE_SMS` at runtime? Please [edit] your question to show us the `` entries from your manifest, and the runtime permissions request code. – Mike M. Sep 03 '19 at 19:24
  • I added manifest code that you want to see, thank you – zulu_papa Sep 04 '19 at 10:16
  • OK, so you haven't done any runtime permissions requests? Since Marshmallow, dangerous permissions must be requested at runtime, too. All of the SMS permissions are dangerous, so you need to request those, and since Oreo, you must explicitly request each one needed. [Here is the relevant developer page](https://developer.android.com/training/permissions/requesting.html), but [this post](https://stackoverflow.com/q/34342816) has some straightforward examples of how to request those all at once, and how to receive the results from the requests. – Mike M. Sep 04 '19 at 10:40
  • I did allow permissions, will update if i get any progress – zulu_papa Sep 04 '19 at 14:24

2 Answers2

0

you have to get the user's permission on runtime with below pseudo code :

public static void requestPermissionForReadSMS(Fragment fragment) {
        if (fragment.shouldShowRequestPermissionRationale(Manifest.permission.READ_SMS)) {
            Helpers.showRequestPermissionAlertDialog(fragment, fragment.getString(R.string.read_sms_permission), fragment.getString(R.string.permission_request));

        } else {
        fragment.requestPermissions(new String[]{Manifest.permission.RECEIVE_SMS},
                Constants.READ_SMS_PERMISSION);
        }

}

after that your BroadcastReceiver will work

also you can add BROADCAST_SMS to your receiver tag inside your manifest :

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

in addition you can read about "SMS Retriever" api witch is provided by official document for receiving sms in your application

Russell Ghana
  • 2,963
  • 3
  • 20
  • 31
  • Sorry, it doesn't solve the problem. There are some strings which can't be defined. I believe those are meant to be messages for the permission. Cant resolve READ_SMS_PERMISSION constant within else block and .showRequestPermissionAlertDialog cant be indentified – zulu_papa Sep 02 '19 at 13:31
-1

I was completely missing the point on my question. The mistake that was producing wrong behavior was tied to intent for receiving messages. I thought that i could use same intent for printing sent messages and receiving messages. In the end when i created custom intent for receiving an sms and it worked.

zulu_papa
  • 415
  • 6
  • 12