0

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:

Android Project Structure SMS receive

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?

jros
  • 714
  • 1
  • 10
  • 33
  • You have the permission listed correctly in the manifest, but on that API level, if your `targetSdkVersion` >= 23, it won't actually be granted at installation. http://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it – Mike M. Nov 21 '16 at 18:14
  • @MikeM. Would I be seeing errors in the Android Monitor if this was the issue? – jros Nov 21 '16 at 18:24
  • I wouldn't think so; maybe info messages, but it's not really an error that your app hasn't been granted a permission. For testing, just drop your `targetSdkVersion` to 22 temporarily. – Mike M. Nov 21 '16 at 18:27
  • @MikeM. That was it! If you make this into an answer I can accept it. – jros Nov 21 '16 at 20:59
  • Actually, I'll just mark this as a duplicate of that one, since I would basically be repeating what that says anyway. Thanks, though. Glad you got it working. Cheers! – Mike M. Nov 22 '16 at 00:00

0 Answers0