Hello stackoverflow community,
I want to ask you a question since I’m stuck since a moment and no solutions work.
I have almost finished my Android Studio project, I display a Toast.makeText and logs to check the reception of a SMS. It works on emulator with SMS sending simulator.
But when I put the app on a phone (xiaomi or samsung) and I receive a real SMS, neither the makeText nor the logs are displayed (I do not enter the onReceive)
Do you have any idea or have you encountered the same problem?
Here is my AndroidManifest :
<?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-permission android:name="android.permission.READ_CONTACTS" />
<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: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.MsgPreparer"
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>
<service
android:name="SMSService"
android:label="Service de SMS MsgPreparer" />
<receiver android:name="SMSReceiver" android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
Here is my SMSService
public class SMSService extends Service {
private static final String TAG = "SMSService";
private boolean active;
private int countSMSReceived;
private SMSReceiver smsReceiver;
@Override
public void onCreate() {
super.onCreate();
active = true;
smsReceiver = new SMSReceiver();
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(smsReceiver, filter);
Log.d(TAG, "SMSService created and SMSReceiver registered");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "SMSService started");
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(smsReceiver);
Log.d(TAG, "SMSReceiver unregistered");
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
And here is my SMSReceiver :
public class SMSReceiver extends BroadcastReceiver {
private static final String TAG = "SMSReceiver";
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "something is happening");
}
}
If someone has any idea/hint to help me, I would be glad to know !
I tried to prevent the other problems I saw on internet, but all seems okay (permissions ect…)
Thanks