So, i am trying to write an app which will send SMS messages, receive and see the messages that have been sent. So far i can send a message, but receiving messages is not working on API 26. My next emulator is API 21 and on that version Toast message appears when message is received.
I have successfully managed permissions required for sending and receiving a sms message.
I have read some documentation, but can't quite find what i need. If there is someone who knows what the problem is i would be thankful for help.
public class SmsReceiver extends BroadcastReceiver {
private static final String TAG = "SmsReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
String message = "";
for(int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
if(i == 0) {
message += messages[i].getOriginatingAddress() + ": ";
}
message += messages[i].getMessageBody();
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
}
}
}
Manifest file
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_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/AppTheme">
<receiver android:name="package.name"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I hope this is enough, if needed i can provide other class or manifest file. Thanks in advance