0

I am writing an app that listens to incoming SMS. I have checked a lot of questions and answers online but it still doesn't work. My codes:

My AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="superapp.networkapp">
<!--send and read SMS-->
<uses-permission android:name="android.permission.SEND_SMS" />
<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=".activity.MainActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:screenOrientation="portrait">
    </activity>

    <receiver android:name=".utility.tool.SmsReceiver"  >
        <intent-filter android:priority="2147483647">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
    <activity
        android:name=".activity.StartTestActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:screenOrientation="portrait"
        tools:replace="screenOrientation">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

My Utility/Tools/SmsReceiver.java

package superapp.networkapp.utility.tool;

import ...

public class SmsReceiver extends BroadcastReceiver {
    //referenced from https://stackoverflow.com/questions/7089313/android-listen-for-incoming-sms-messages
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Log.d("SMS", "onReceive: ");
        if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
            //---get the SMS message passed in---
            Bundle bundle = intent.getExtras();           
            SmsMessage[] messages = null;
            String sender;
            if (bundle != null) {
                //---retrieve the SMS message received---
                try {
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    if (pdus != null) {
                        messages = new SmsMessage[pdus.length];
                        for(int i=0; i<messages.length; i++){
                            messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]); //TODO
                            sender = messages[i].getOriginatingAddress();
                            String msgBody = messages[i].getMessageBody();
                            Toast.makeText(context,"Message: "+ msgBody + " from " + sender,Toast.LENGTH_LONG).show();
                        }
                    }
                } catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}

I have askPermissions() function running in runtime

   private static final List<String> permissions = Arrays.asList(
            Manifest.permission.SEND_SMS,
            Manifest.permission.READ_SMS,
            Manifest.permission.RECEIVE_SMS);
   private void askPermissions() {
        if (!hasAllPermissions())
            ActivityCompat.requestPermissions(this, permissions.toArray(new String[0]), 69);
    }
    private boolean hasPermission(String permission) {
        return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED;
    }

    private boolean hasAllPermissions() {
        for (String permission : permissions) {
            if (!hasPermission(permission)) return false;
        }
        return true;
    }

I have 3 devices : A Samsung s7 and an Sony Xperia Z5 Compact both using Android 7.0, and a Samsung s6 using Android 6.0.1. When I send messages to those devices, only the default messenger apps show the notifications.

I have checked other people's answers and questions. It seems like the default messenger apps have higher intentFilter priority or they call abortBroadcast(). I have also set the priority of my intent to be the largest integer number or 999 as defined in Android documentation but no luck yet.

phuwin
  • 3,130
  • 4
  • 26
  • 49

1 Answers1

1

You can try this, i also had this same issue, fixed by adding permission. and dont need to register your receiver programmatically, but you need to ask permissions Manifest.permission.READ_SMS

    <receiver
        android:name=".name"
        android:permission="android.permission.BROADCAST_SMS">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
Sanoop Surendran
  • 3,484
  • 4
  • 28
  • 49