Search code examples
androidbroadcastreceivertoast

Checking if a Broadcast Receiver is Working


I am working on an app that will use a BroadcastReceiver to pick up certain SMS messages that meet certain criteria. It's a long way short of finished, mainly because it seems that the BroadcastReceiver isn't working. I've tried to use a toast to check if it's working but I get no result. So either:

  1. The BroadcastReceiver is not working
  2. My method of testing is wrong
  3. Or both

The AndroidManifest.xml file is

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

    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" />

    <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/Theme.Alert6">

        <activity
            android:name=".SendResponseActivity"
            android:parentActivityName=".ReceiveAlertActivity">
        </activity>

        <activity
            android:name=".ReceiveAlertActivity"
            android:parentActivityName=".MainActivity">
        </activity>

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

        <receiver
            android:name=".SmsBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="999" >
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

    </application>

</manifest>

And this is the SmsBroadcastReceiver java file

package com.example.alert6;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Telephony;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"SMS Is Being Received",Toast.LENGTH_LONG).show();
        if (intent.getAction().equals(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) {
            String smsSender = "";
            String smsBody = "";
            for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
                smsSender = smsMessage.getOriginatingAddress();
                smsBody = smsMessage.getMessageBody();
            }

            if (smsSender.equals("+420775367297")) {
                if (smsBody.contains("Test")) {
// I haven't done this bit yet
                }
            }
        }
    }
}

When I send a SMS to the test device I would expect a toast message saying "SMS Is Being Received". Instead the app disappears from the screen and my default SMS app appears instead. What am I doing wrong?


Solution

  • Since Android API 23 you need set permission not only in manifest class, but also set permisson manually. You should set it in app's settings, or you should make permission reqeust from your code. This is what you need add in your main activity's file:

    // constant for request code
    private final int MY_PERMISSIONS_REQUEST_SMS_RECEIVE = 10; // any number which you want
    
    //function for permission request
    @RequiresApi(api = Build.VERSION_CODES.M)
    public void checkPermission() {
        if ((ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED)) {
            requestPermissions(new String[]{Manifest.permission.RECEIVE_SMS}, MY_PERMISSIONS_REQUEST_SMS_RECEIVE);
        }
    }
    

    Also you need call this function somewhere. For example you can make it in onCreate().

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            checkPermission();
        }
        //do something else
    }