1

I am trying to receive SMS's on my app, and I am testing on Android Studio's emulator. It seems to me that I have written what I had to write in order to receive them:

  • I made sure the app is the default SMS app (through copying the sample code in an Android Developer blog tutorial),
  • I put an intent filter for SMS_DELIVER_ACTION in the manifest, with the permission for BROADCAST_SMS and the priority set to 999,
  • I created a class named SmsReceiver inheriting BroadcastReceiver, and put a onCreate(Context, Intent) function in it.

However, the receiver named SMSReceiver doesn't appear to be invoked when I send a SMS through the Emulator's extended controls... I do know they work, as, when the app Messages is the default SMS app, the SMS shows up in the notifications.)

The way I see it is that the Toast that I put in the onReceive function doesn't show up. I also put two breakpoints in the onReceive function in Android Studio, but they don't appear to be reached.

(The emulated device is a Pixel XL with API 26 / Android O. I am on macOS 10.13 High Sierra.)

What am I doing wrong?

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="st.thetechnologi.smscopy">

    <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"
        android:minSdkVersion="19">

        <receiver android:name=".SmsReceiver"
            android:enabled="true"
            android:permission="android.permission.BROADCAST_SMS">
            <intent-filter android:priority="999">
                <action android:name="android.provider.Telephony.SMS_DELIVER" />
            </intent-filter>
         </receiver>

        <!-- BroadcastReceiver that listens for incoming MMS messages -->
        <receiver android:name=".MmsReceiver"
            android:permission="android.permission.BROADCAST_WAP_PUSH">
            <intent-filter>
                <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
                <data android:mimeType="application/vnd.wap.mms-message" />
            </intent-filter>
        </receiver>

        <!-- Activity that allows the user to send new SMS/MMS messages -->
        <activity android:name=".ComposeSmsActivity" >
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.SENDTO" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </activity>

        <!-- Service that delivers messages from the phone "quick response" -->
        <service android:name=".HeadlessSmsSendService"
            android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </service>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".DisplayMessageActivity"
            android:parentActivityName=".MainActivity" >
         </activity>
    </application>

</manifest>

SmsReceiver.java

package st.thetechnologi.smscopy;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

/**
 * Created by l on 10/22/17.
 */

public class SmsReceiver extends BroadcastReceiver {
    private static final String TAG = "SmsReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, TAG, Toast.LENGTH_LONG).show();
    }
}

Thank you!

EDIT: OK, I needed to grant the SMS permission manually from Settings to my app. Thanks to Mike M. to put me on the right trail!

  • @NabinBhandari https://stackoverflow.com/questions/29698546/how-send-a-message-to-the-emulator-using-ddms-in-android-studio] – 10101010 Oct 22 '17 at 16:22
  • If that wasn't working on a real device, then that was a bug but if you want to send SMS to an Android emulator (which have no SIMs) then it's your problem. – isamirkhaan1 Oct 22 '17 at 16:25
  • add android:exported=true ? and did you launch the app before sending the sms? Does it work after removing priority? if you want it to work without launching an app maybe try adding this inside boradcastreceiver – 10101010 Oct 22 '17 at 16:27
  • Thank for your comments, but the problem doesn't seem to come from the fact that I am sending SMS from the emulator... Once again, when I send a SMS with the Messages app as default SMS app, the message shows up exactly as expected. – Léopold Mebazaa Oct 22 '17 at 16:30
  • I never knew it's possible to set your app as the default without the necessary permissions, but that's your problem: https://stackoverflow.com/a/30133663. I just tested, and though you can set it as the default, it throws everywhere a permission is needed. – Mike M. Oct 22 '17 at 16:31
  • @MikeM. (Edit) I put the `` just before the first `` tag, no better luck... – Léopold Mebazaa Oct 22 '17 at 16:33
  • @10101010 I did launch the app before. `MainActivity` corresponds to the basic UI specified in the [Getting Started tutorial](https://developer.android.com/training/basics/firstapp/building-ui.html) – Léopold Mebazaa Oct 22 '17 at 16:41
  • Hmm, setting your app as the default should've granted that permission, I believe. Did you already have your app installed and set as the default when you added the permissions to the manifest? – Mike M. Oct 22 '17 at 17:11
  • 1
    Yep! That's exactly what happened. – Léopold Mebazaa Oct 22 '17 at 20:09

0 Answers0