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 forBROADCAST_SMS
and the priority set to 999, - I created a class named
SmsReceiver
inheritingBroadcastReceiver
, and put aonCreate(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!