In my Android application I am trying to use BroadcastReceiver
to listen to incoming SMS and phone calls.
But for some reason the onReceive
method is never called when I get an SMS.
To make sure the receiver works, I tried to use "android.intent.action.AIRPLANE_MODE"
intent filter since it seems the simplest one to trigger.
This is my AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
<uses-feature android:name="android.hardware.telephony" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.CallsListener"
tools:targetApi="31" >
<activity
android:name=".MainActivity"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And this is the receiver class:
package com.sagiziv.callslistener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.SmsMessage;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
import java.util.Date;
public class MessagesReceiver extends BroadcastReceiver {
public MessagesReceiver() {
super();
Log.d("pttt", "Receiver created");
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d("pttt", "onReceive: " + intent.getAction());
Toast.makeText(context, intent.getAction(), Toast.LENGTH_LONG).show();
Bundle extras = intent.getExtras();
if (extras != null) {
if (intent.getAction().equals(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) {
// Handle incoming SMS
SmsMessage[] msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent);
}
}
}
}
I am registering the receiver in the main activity like so:
package com.sagiziv.callslistener;
import android.Manifest;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final int PERMISSION_REQUEST_CODE = 123;
private MessagesReceiver myReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermissions();
}
private void requestPermissions() {
if (checkSelfPermission(Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{
Manifest.permission.READ_SMS,
Manifest.permission.READ_PHONE_STATE
}, PERMISSION_REQUEST_CODE);
} else {
myReceiver = new MessagesReceiver();
// Register MyReceiver for SMS
registerReceiver(myReceiver, new IntentFilter("android.intent.action.AIRPLANE_MODE"));
registerReceiver(myReceiver, new IntentFilter(Telephony.Sms.Intents.SMS_RECEIVED_ACTION));
}
}
@Override
protected void onDestroy() {
// Unregister MyReceiver when the activity is destroyed
if (myReceiver != null) {
unregisterReceiver(myReceiver);
Log.d("pttt", "Receiver unregistered");
}
super.onDestroy();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_REQUEST_CODE) {
// Check if permissions are granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Denied", Toast.LENGTH_LONG).show();
}
}
}
}
As you can see I make sure I have the permissions to access SMS, but none of the logs is printed when I get an SMS and I don't understand why.
I tested it on my phone that runs Android version 8.0.0, and on my tablet which runs Android version 13.
Well after more searching, I discovered that my mistake was with the permissions.
I requested Manifest.permission.READ_SMS
permission instead of Manifest.permission.RECEIVE_SMS
permission.
Changing that solved the issue.