I am trying to have my app detect and read all incoming messages. Later on, I'll filter this down significantly, but for now I'm keeping it broad. I have been looking through stackoverflow posts and I found this class:
package com.example.hsport.catalog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
public class SmsListener extends BroadcastReceiver{
private SharedPreferences preferences;
@Override
public void onReceive(Context context, Intent intent) {
Log.d("onReceive", "Message was received.");
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String msg_from;
if (bundle != null){
try{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
Log.d("onReceive", msgBody);
}
}catch(Exception e){
Log.d("Exception caught",e.getMessage());
}
}
}
}
}
Here is where this class exists in my project structure:
As far as I am aware, I have added the necessary elements to the manifest file, including the required app permissions and the <receiver> for SMS_RECEIVED:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hsport.catalog" >
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsListener">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
So after doing all of this, my expectation would be that after building/compiling the app, pushing it to my device, and then opening it on my device, all SMS messages that are received would trigger the SmsListener.onReceive() method, and I would see some debugging log messages show up in the console on Android Studio. I have also tried setting breakpoints inside of OnReceive(), but none of them are getting hit.
However, I am not seeing either of the logging messages seen in SmsListener show up in the Android Monitor window. I am using my Google Pixel XL as a debugging device, not a virtual device. To test if it's working, I'm sending myself a text message and watching the output on the Android Monitor, with the first dropdown set to Google Pixel XL (Android 7.1 API 25) and the second dropdown set to com.example.hsport.catalog.
What am I doing wrong and how do it fix the problem so that SMS messages received on my phone while my app is open (background or active) trigger the onReceive() event in SmsListener?